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

Dave Angel d at davea.name
Wed Feb 29 11:30:10 CET 2012


On 02/29/2012 04:05 AM, Guillaume Chorn wrote:
> Thanks, the suggestion
>
> print name.decode("utf-8").strip()
>
> worked like a charm.  When I did the print repr(name) I got exactly what
> you predicted.  I'm not yet sure what all of this means, but I'm going to
> read this<http://docs.python.org/howto/unicode.html>  in the hopes of
> finding out.  Anyway thanks again for the help!
>
>
> cheers,
> Guillaume
>

What it means is that the character before the name was not a space 
character, but some Unicode variant of space.  Peter figured that out, 
not I.  It also means that you were using the incorrect decoder for the 
way the file was encoded and written out.

Are you creating that csv file with some Windows app, or with Python?  
You should look at it with a hex viewer to see if there are other 
surprises waiting for you in there.  For example, you might also need 
the same kind of solution if there are quotes (or an apostrophe) 
anywhere in the file. Or if someone's address uses an umlaut.

Generally, it's better to clean up cruft at the source, when possible.  
And if that means the file has to be stored in utf-8, you should decode 
the whole file that way, and then deal with it as unicode.





-- 

DaveA




More information about the Python-list mailing list