unicode compare errors

Ross rossgk at gmail.com
Fri Dec 10 15:07:19 EST 2010


On Dec 10, 2:51 pm, Ross <ros... at gmail.com> wrote:

> Initially I was simply doing:
>
>   currs = [u'$', u'£', u'€', u'¥']
>   aFile = open(thisFile, 'r')
>   for mline in aFile:              # mline might be "£5.50"
>      if item[0] in currs:
>           item = item[1:]
>

Don't you love it when someone solves their own problem?  Posting a
reply here so that other poor chumps like me can get around this...

I found I could import codecs that allow me to read the file with my
desired encoding. Huzzah!

Instead of opening the file with a standard
   aFile = open(thisFile, 'r')

I instead ensure I've imported the codecs:

    import codecs

... and then I used a specific encoding on the file read:

    aFile = codecs.open(thisFile, encoding='utf-8')

Then all my compares seem to work fine.
If I'm off-base and kludgey here and should be doing something
differently please give me a poke.

Regards,
Ross.



More information about the Python-list mailing list