Python, Unicode, Excel and Web Tesing

John J. Lee jjl at pobox.com
Fri Nov 7 14:10:47 CET 2003


calfdog at yahoo.com writes:
[...]
> I have been running into problems with Unicode as I can see others
> have too.
> I have seemed to find a simple solution when printing
> 
> Since Excel seems to give back this format  (u'Test1',)
> 
> If I use this code I get
> (u'Test1',)
> (u'Test2',)
> (u'Test3',)
> (u'Test4',)
> (u'Test5',)
[...]
> for i in val:
>     print i   #Prints the unicode charaters

These are length-1 tuples containing unicode strings.


[...]
> If I use the code below  it works fine.
> Test1
> Test2
> Test3
> Test4
> Test5
[...]
> for i in val:
>     print i [-1]  #Prints without the unicode charaters

These are unicode strings.  Why no u'' business?  First, you need to
know that str()'s job is to print readable strings.  repr(obj)'s job
is to print a string such that when you eval() it, you get back the
same thing you started out with:

>>> text = repr(u'foo')
>>> eval(text)
u'foo'
>>>

ie. repr(u'foo') == "u'foo'"
whereas str(u'foo') == "foo"

Now, print obj prints str(obj), but str(sequence) typically returns a
string containing the repr() of every item in the sequence, NOT the
str() of every item in the sequence.  Hence:

>>> str(u'foo')
'foo'
>>> str((u'foo',))
"(u'foo',)"
>>>


John




More information about the Python-list mailing list