[Tutor] FW: extract specific column

Steven D'Aprano steve at pearwood.info
Thu Oct 13 01:26:11 CEST 2011


Anna Olofsson wrote:
> The thing is, I don't know where to start. I know how to open the
> attached file, but I don't know how to work inside the file. Is this
> a csv file? Do I need to use a split function? In what way should I
> look at the file? As a string? As lines?


It looks like a CSV file. Try opening it in Excel or OpenOffice 
spreadsheet and check that they are happy opening it as CSV. You'll 
probably need to play with the settings when you do, e.g. change the 
separator to tab instead of comma, etc. If you can open the file in 
Excel and get sensible results, you should be able to do the same thing 
with Python and the csv module.

You can learn more about the csv module here:

http://effbot.org/librarybook/csv.htm
http://www.doughellmann.com/PyMOTW/csv/

After taking a quick glance at the file you provided, it looks to me 
like fixed width fields (padded with spaces) in a tab-separated format. 
So you might have to do some post-processing to get rid of the spaces. 
That's pretty easy: any time you extract a value, just call 
value.strip() to remove the spaces before doing anything else with it.



-- 
Steven


More information about the Tutor mailing list