Problem using s.strip() to remove leading whitespace in .csv file

Karim kliateni at gmail.com
Wed Feb 29 10:04:59 CET 2012


Le 29/02/2012 10:01, Karim a écrit :
> Le 29/02/2012 09:25, Guillaume Chorn a écrit :
>> Hello All,
>>
>> I have a .csv file that I created by copying and pasting a list of 
>> all the players in the NBA with their respective teams and positions 
>> (http://sports.yahoo.com/nba/players?type=lastname&first=1&query=&go=GO! 
>> <http://sports.yahoo.com/nba/players?type=lastname&first=1&query=&go=GO%21>).  
>> Unfortunately, when I do this I have no choice but to include a 
>> single leading whitespace character for each name.  I'd like to 
>> compare this list of names to another list (also in .csv format but 
>> in a different file) using a simple Python script, but in order to do 
>> that I need to strip the leading whitespace from all of the names or 
>> none of them will match the names in the second list.  However, when 
>> I try to do this as follows:
>>
>> positions = open('/home/guillaume/Documents/playerpos.csv')
>>
>> for line in positions:
>>     info = line.split(',')
>>     name = info[0]
>>     name = name.strip()
>>     print name #to examine the effect of name.strip()
>>
>> I keep getting all of the names with the leading whitespace character 
>> NOT removed (for example: " Jeff Adrien". Why is this happening?
>>
>> The following is a sample of the .csv file (the one I'm trying to 
>> remove the whitespace from) opened in gedit, the built-in Ubuntu text 
>> editor:
>>
>>  Jeff Adrien,SF,Houston Rockets
>>  Arron Afflalo,SG,Denver Nuggets
>>  Maurice Ager,GF,Minnesota Timberwolves
>>  Blake Ahearn,PG,Los Angeles Clippers
>>  Alexis Ajinca,FC,Toronto Raptors
>>  Solomon Alabi,C,Toronto Raptors
>>  Cole Aldrich,C,Oklahoma City Thunder
>>  LaMarcus Aldridge,FC,Portland Trail Blazers
>>  Joe Alexander,SF,New Orleans Hornets
>>  Lavoy Allen,FC,Philadelphia 76ers
>>  Malik Allen,FC,Orlando Magic
>>  Ray Allen,SG,Boston Celtics
>>  Tony Allen,GF,Memphis Grizzlies
>>  Lance Allred,C,Indiana Pacers
>>  Rafer Alston,PG,Miami Heat
>>
>> Any help with this seemingly simple but maddening issue would be much 
>> appreciated.
>>
>> thanks,
>> Guillaume
>>
>>
>>
>>
>>
> Use csv module instead of parsing yourself the csv file:
>
> import csv
>
> reader = csv.Reader(open("file.csv"))
>
> for row in reader:
>       for key in row:
>             print("key=", key, " value=", row[key])
>
>
> Code above not tested but should work.
>
> Cheers
> Karim
>
Just a correction use a dictionary like csv reader:

*reader = csv.DictReader(open("file.csv"))*

Cheers
Karim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120229/c2cd52fc/attachment.html>


More information about the Python-list mailing list