2

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?

2
  • Do you mean "sort it by the middle column"? Commented Jul 24, 2013 at 6:40
  • You could do this very eficiently with a few *nix pipes. Commented Jul 24, 2013 at 6:48

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.