[Tutor] how to sort a tab delim file

wesley chun wescpy at gmail.com
Wed Jan 13 21:12:16 CET 2010


> I have a tab-delim file:
>
> col1         col2     col3
> andrew    1987   1990
> jake         1974   1980
> jim           1964   1970
> lance        1984   1992
>
> how can I sort column 2 and get :
> jim           1964   1970
> jake         1974   1980
> lance        1984   1992
> andrew    1987   1990
>
> I know how to sort a items in one list. but I cannot get how this can be
> done on a file.


hi, it seems like you almost have all the tools you need to solve this
problem! here are your tasks:

1. read the data out of a file and into a list -- experiment with
open() and listcomps
2. sort the list -- you already know how to do this
3. create a new file with the sorted data - use open() again,
listcomps, str.join(), and file.write()

good luck!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list