help I'm getting delimited

MRAB google at mrabarnett.plus.com
Tue Dec 16 18:50:15 CET 2008


Paul Watson wrote:
> On Tue, 2008-12-16 at 08:26 -0800, aka wrote:
>> Hi, I'm going nuts over the csv.reader and UnicodeReader class.
>> Somehow I can't get this method working which is supposed to read a
>> csv file which name is inputted but here now hardcoded. What I need
>> for now is that the string version of the list is put out for control.
>> Later on I will only need to read the first column (id) of the csv
>> file to be able to fill in a session var with a list of all ids.
>> inp = c:/temp/test.csv
>> roles = []
>> try:
>> fp = open(inp, 'rb')
>> reader = csv.reader(fp)
>> for r in reader:
>>     rollen.append(r)
>> except:
>>     msg = "Er is iets mis met de UnicodeReader"
>>
>> return dict(file=in,roles=str(roles))
>> Any help greatly appreciated!
>> Cheers
> 
> Did you intend inside the loop to write:
> 
> 	roles.append(r)
> 
Also, the bare "except" will catch _all_ exceptions. You should catch 
only those you expect. In this case, it's catching your use of "rollen" 
instead of "roles" (probably unintentional) and then complaining about 
UnicodeReader, even though that's (probably, again!) not the problem.



More information about the Python-list mailing list