tags:

views:

81

answers:

2

Hi, having problem with UnicodeEncodeError('ascii', u'Phase \u2013 II', 6, 7, 'ordinal not in range(128)') Basically what I am doing here is reading the value from excel sheet and sheet contain address in this format

Phase- II

So wanted to know how to change`

somestring = u'Phase \u2013 II'

to str

thanks

`

A: 

Sure there is a better way, but:

>>somestring = u'Phase \u2013 II'
>>a = somestring.encode('utf-8')
>>a.replace('\xe2\x80\x93', '-')
'Phase - II'
joaquin
Can this be done in generic way.I mean here i know the string contains '-' and replacing it.
laspal
+1  A: 

Excel mostly uses cp1252, so try this:

>>> somestring.encode('cp1252', 'replace')
'Phase \x96 II'
>>> print somestring.encode('cp1252', 'replace')
Phase – II

That doesn't give you an ascii string (since your unicode string contains non-ascii characters it cannot), but it does give you a byte string that Excel will interpret correctly if for example you write it into a csv file.

If you just want to print it for display then you'll need to know the output encoding of whatever you use to display the text: I copied the example from idle which will, at least on my system displays cp1252, but if you print it in a command prompt you may have another encoding in effect. Use the DOS chcp command to select an appropriate encoding if required as the default encoding may not support that character:

C:\>chcp
Active code page: 850

C:\>\python26\python
Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> somestring = u'Phase \u2013 II'
>>> print somestring.encode('cp850', 'replace')
Phase ? II
>>>

Using the 'replace' argument to encode means that if you do manage to get any characters that cannot be interpreted as cp1252 will be replaced by question marks.

Duncan
"Excel mostly uses cp1252": Not so. Starting with Excel 97 (as in the year 1997), Excel supports Unicode (BMP only AFAIK). Each piece of text is encoded as ISO-8859-1 if possible otherwise as UTF-16LE, with a bit to say which. Earlier Excels encoded all text according to a particular encoding, the codepage number of which was recorded in the the file. In any case, once you have a unicode object in Python, where you got it from is irrelevant. What you need to know is what encoding should be used to display it to the user ... and yes, cp1252 and cp850 are the usual suspects.
John Machin