<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Le 29/02/2012 10:01, Karim a écrit :
<blockquote cite="mid:4F4DE961.2060302@gmail.com" type="cite">
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
Le 29/02/2012 09:25, Guillaume Chorn a écrit :
<blockquote
cite="mid:CAJbu0og=0K4yzfe9ZDzyTtWzNaUxSjeUE8XqRw8s7VgUgQih5Q@mail.gmail.com"
type="cite">Hello All,<br>
<br>
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 (<a moz-do-not-send="true"
href="http://sports.yahoo.com/nba/players?type=lastname&first=1&query=&go=GO%21">http://sports.yahoo.com/nba/players?type=lastname&first=1&query=&go=GO!</a>).
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:<br>
<br>
positions = open('/home/guillaume/Documents/playerpos.csv')<br>
<br>
for line in positions:<br>
info = line.split(',')<br>
name = info[0]<br>
name = name.strip()<br>
print name #to examine the effect of name.strip()<br>
<br>
I keep getting all of the names with the leading whitespace
character NOT removed (for example: " Jeff Adrien". Why is this
happening?<br>
<br>
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:<br>
<br>
Jeff Adrien,SF,Houston Rockets<br>
Arron Afflalo,SG,Denver Nuggets<br>
Maurice Ager,GF,Minnesota Timberwolves<br>
Blake Ahearn,PG,Los Angeles Clippers<br>
Alexis Ajinca,FC,Toronto Raptors<br>
Solomon Alabi,C,Toronto Raptors<br>
Cole Aldrich,C,Oklahoma City Thunder<br>
LaMarcus Aldridge,FC,Portland Trail Blazers<br>
Joe Alexander,SF,New Orleans Hornets<br>
Lavoy Allen,FC,Philadelphia 76ers<br>
Malik Allen,FC,Orlando Magic<br>
Ray Allen,SG,Boston Celtics<br>
Tony Allen,GF,Memphis Grizzlies<br>
Lance Allred,C,Indiana Pacers<br>
Rafer Alston,PG,Miami Heat<br>
<br>
Any help with this seemingly simple but maddening issue would be
much appreciated.<br>
<br>
thanks,<br>
Guillaume<br>
<br>
<br>
<br>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
</blockquote>
Use csv module instead of parsing yourself the csv file:<br>
<br>
import csv<br>
<br>
reader = csv.Reader(open("file.csv"))<br>
<br>
for row in reader:<br>
for key in row:<br>
print("key=", key, " value=", row[key])<br>
<br>
<br>
Code above not tested but should work.<br>
<br>
Cheers<br>
Karim<br>
<br>
</blockquote>
Just a correction use a dictionary like csv reader:<br>
<br>
*reader = csv.DictReader(open("file.csv"))*<br>
<br>
Cheers<br>
Karim<br>
</body>
</html>