how to sort large files in python

Solutions on MaxInterview for how to sort large files in python by the best coders in the world

showing results for - "how to sort large files in python"
Giovanni
08 Jan 2021
1def build_index(filename, sort_col):
2    index = []
3    f = open(filename)
4    while True:
5        offset = f.tell()
6        line = f.readline()
7        if not line:
8            break
9        length = len(line)
10        col = line.split('\t')[sort_col].strip()
11        index.append((col, offset, length))
12    f.close()
13    index.sort()
14    return index
15
16def print_sorted(filename, col_sort):
17    index = build_index(filename, col_sort)
18    f = open(filename)
19    for col, offset, length in index:
20        f.seek(offset)
21        print f.read(length).rstrip('\n')
22
23if __name__ == '__main__':
24    filename = 'somefile.txt'
25    sort_col = 2
26    print_sorted(filename, sort_col)