All the following code does is: it reads a list in a file, sorts it out by the second column and writes the output in a new file.
import operator
original_flightevent = 'flightevent.out.1'
new_sorted_flightevent = 'flightevent.sorted.out.1'
readfile1 = open(original_flightevent, 'r')
writefile1 = open(new_sorted_flightevent, 'a')
writefile2 = open('flightevent.sorted.out.2', 'a')
def sort_table(table, col=1):
return sorted(table, key=operator.itemgetter(col), reverse=False)
if __name__ == '__main__':
data = (line.strip().split(';') for line in readfile1)
for line in sort_table(data, 1):
print >> writefile1, line
readfile2 = open(new_sorted_flightevent, 'r')
numline=1
for i in range(numline):
readfile2.next()
for line in readfile2:
p=line
if p:
writefile2.write(p)
What's the best way to avoid writing the outputfile, so instead storing it in a internal list?