Simd
Simd

Reputation: 21223

Processing files in memory

I have a file with each line like

Name1 name2 name3

I can read it in line by line, split each line, process the line and output the processed line by line.

However I would like to read the whole file in, sort it by the middle column, then output the whole sorted file in the same format the input was in.

My first attempt splits each line as it is read in to make a list of tuples, then sorts using key= , then rejoins each tuple and outputs line by line.

Is there a more pythonic way of doing this?

Upvotes: 2

Views: 76

Answers (1)

mgilson
mgilson

Reputation: 309929

Something like this ought to do:

with open('datafile') as fin:
    lines = list(fin)
    lines.sort(key=lambda line: line.split()[1])

with open('outfile','w') as fout:
    fout.writelines(lines)

A few notes:

  • My sort key is a bit ugly. However, the advantage here is that it preserves the lines exactly as they were in the input file. If we split the lines and then sorted, the code might be a little prettier, but we would need to join the split lines correctly on output (plus, runs of whitespace might be lost)

  • outfile can be the same filename as datafile to make the change effectively "in place"

  • If you need to support quoting of fields (field1 "field 2" field3), then you'll want to look at the csv module.

Upvotes: 4

Related Questions