12

I'm trying to deal with unicode in python 2.7.2. I know there is the .encode('utf-8') thing but 1/2 the time when I add it, I get errors, and 1/2 the time when I don't add it I get errors.

Is there any way to tell python - what I thought was an up-to-date & modern language to just use unicode for strings and not make me have to fart around with .encode('utf-8') stuff?

I know... python 3.0 is supposed to do this, but I can't use 3.0 and 2.7 isn't all that old anyways...

For example:

url = "http://en.wikipedia.org//w/api.php?action=query&list=search&format=json&srlimit=" + str(items) + "&srsearch=" + urllib2.quote(title.encode('utf-8'))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 19: ordinal not in range(128)

Update If I remove all my .encode statements from all my code and add # -*- coding: utf-8 -*- to the top of my file, right under the #!/usr/bin/python then I get the following, same as if I didn't add the # -*- coding: utf-8 -*- at all.

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py:1250: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  return ''.join(map(quoter, s))
Traceback (most recent call last):
  File "classes.py", line 583, in <module>
    wiki.getPage(title)
  File "classes.py", line 146, in getPage
    url = "http://en.wikipedia.org/w/api.php?action=query&prop=revisions&format=json&rvprop=content&rvlimit=1&titles=" + urllib2.quote(title)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 1250, in quote
    return ''.join(map(quoter, s))
KeyError: u'\xf1'

I'm not manually typing in any string, I parsing HTML and json from websites. So the scripts/bytestreams/whatever they are, are all created by python.

Update 2 I can move the error along, but it just keeps coming up in new places. I was hoping python would be a useful scripting tool, but looks like after 3 days of no luck I'll just try a different language. Its a shame, python is preinstalled on osx. I've marked correct the answer that fixed the one instance of the error I posted.

5
  • FYI, I just posted a related question that drills down into an aspect of this one: stackoverflow.com/questions/12557447/…
    – Mu Mind
    Commented Sep 24, 2012 at 0:47
  • 6
    Please read joelonsoftware.com/articles/Unicode.html . Now. A person won't be able to make a working program using text at all nevermind dealing properly with encoding conversions if he doesn't understand at least what is in this article. From your question wording it is clear you are making blind attempts.
    – jsbueno
    Commented Sep 24, 2012 at 2:04
  • 2
    @jsbueno - I know what unicode is, I know how it works. Python fubard it up to the point where you have to make blind attempts to use it at all.
    – Justin808
    Commented Sep 24, 2012 at 2:53
  • No you don't. Pytho's way of using it is quite sane if you understand how it works, as it is nicely explained in the above link.
    – jsbueno
    Commented Sep 24, 2012 at 4:26
  • BTW..do not take it as an offensive comment, please. Just read the article and you will be more confident not only to the task at hand, andnot jsut to deal with Python text issues.
    – jsbueno
    Commented Sep 24, 2012 at 4:28

5 Answers 5

20

This is a very old question but just wanted to add one partial suggestion. While I sympathise with the OP's pain - having gone through it a lot myself - here's one (partial) answer to make things "easier". Put this at the top of any Python 2.7 script:

from __future__ import unicode_literals

This will at least ensure that your own literal strings default to unicode rather than str.

1
  • This seems the most direct answer to the OP's (main) question. Commented May 8, 2014 at 13:12
18

There is no way to make unicode "just work" apart from using unicode strings everywhere and immediately decoding any encoded string you receive. The problem is that you MUST ALWAYS keep straight whether you're dealing with encoded or unencoded data, or use tools that keep track of it for you, or you're going to have a bad time.

Python 2 does some things that are problematic for this: it makes str the "default" rather than unicode for things like string literals, it silently coerces str to unicode when you add the two, and it lets you call .encode() on an already-encoded string to double-encode it. As a result, there are a lot of python coders and python libraries out there that have no idea what encodings they're designed to work with, but are nonetheless designed to deal with some particular encoding since the str type is designed to let the programmer manage the encoding themselves. And you have to think about the encoding each time you use these libraries since they don't support the unicode type themselves.


In your particular case, the first error tells you you're dealing with encoded UTF-8 data and trying to double-encode it, while the 2nd tells you you're dealing with UNencoded data. It looks like you may have both. You should really find and fix the source of the problem (I suspect it has to do with the silent coercion I mentioned above), but here's a hack that should fix it in the short term:

encoded_title = title
if isinstance(encoded_title, unicode):
    encoded_title = title.encode('utf-8')

If this is in fact a case of silent coercion biting you, you should be able to easily track down the problem using the excellent unicode-nazi tool:

python -Werror -municodenazi myprog.py

This will give you a traceback right at the point unicode leaks into your non-unicode strings, instead of trying troubleshooting this exception way down the road from the actual problem. See my answer on this related question for details.

8
  • 1
    Ah well, this worked in the one method, but just moved the error along to another spot. Guess I'll just rewrite everything in another language. I had hoped python would be a useful scripting tool, 3 days later, nope.
    – Justin808
    Commented Sep 24, 2012 at 2:48
  • If this gets rid of your error, great! That confirms that your problem is unicode strings getting mixed in with non-unicode. That bad data is still out there somewhere, and the other error is most likely just another symptom of the same original problem. I just updated the answer to mention the unicode-nazi tool, which should let you easily suss out the root problem.
    – Mu Mind
    Commented Sep 24, 2012 at 3:22
  • Looks like HTMLParser doesn't do unicode? I just dont understand why it has to be so very hard to deal with unicode. It should be 100% hidden from the developer, its low level stuff. Heck even obj-c hides it away and eveyrthing just works.
    – Justin808
    Commented Sep 24, 2012 at 3:47
  • The isinstance thing isn't working for def handle_data(self, data): the data returned from the HTMLParser class either.
    – Justin808
    Commented Sep 24, 2012 at 3:49
  • It should be hidden from the developer in most of the cases you're dealing with, but some people had decided that it would be more important for these things to be fast than correct, and the process to change core libraries is so slow that it's taken this long before python 3 got anything done about it.
    – Mu Mind
    Commented Sep 24, 2012 at 4:02
3

Yes, define your unicode data as unicode literals:

>>> u'Hi, this is unicode: üæ'
u'Hi, this is unicode: üæ'

You usually want to use '\uxxxx` unicode escapes or set a source code encoding. The following line at the top of your module, for example, sets the encoding to UTF-8:

# -*- coding: utf-8 -*-

Read the Python Unicode HOWTO for the details, such as default encodings and such (the default source code encoding, for example, is ASCII).

As for your specific example, your title is not a Unicode literal but a python byte string, and python is trying to decode it to unicode for you just so you can encode it again. This fails, as the default codec for such automatic encodings is ASCII:

>>> 'å'.encode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)

Encoding only applies to actual unicode strings, so a byte string needs to be explicitly decoded:

>>> 'å'.decode('utf-8').encode('utf-8')
'\xc3\xa5'

If you are used to Python 3, then unicode literals in Python 2 (u'') are the new default string type in Python 3, while regular (byte) strings in Python 2 ('') are the same as bytes objects in Python 3 (b'').

If you have errors both with and without the encode call on title, you have mixed data. Test the title and encode as needed:

if isinstance(title, unicode):
    title = title.encode('utf-8')

You may want to find out what produces the mixed unicode / byte string titles though, and correct that source to always produce one or the other.

4
  • Hi, I tried # -*- coding: utf-8 -*- but it seems like it doesn't do anything.
    – Justin808
    Commented Sep 23, 2012 at 23:18
  • @Justin808: Read the linked HOWTO. Oh, and this Joel Spolsky article. Your title is a byte string, no need to encode it again. Commented Sep 23, 2012 at 23:19
  • Is this in a loop and is there more than one title? I suspect that some are unicode, some are not. Commented Sep 23, 2012 at 23:21
  • 2
    try if isinstance(title, unicode): title=title.encode('utf-8'); you almost certainly have mixed data. Commented Sep 23, 2012 at 23:39
2

be sure that title in your title.encode("utf-8") is type of unicode and dont use str("İŞşĞğÖöÜü")

use unicode("ĞğıIİiÖöŞşcçÇ") in your stringifiers

1
  • 1
    Not without an explicit encoding you can't. unicode('å') will fail with the same UnicodeDecodeError. Commented Sep 23, 2012 at 23:15
2

Actually, the easiest way to make Python work with unicode is to use Python 3, where everything is unicode by default.

Unfortunately, there are not many libraries written for P3, as well as some basic differences in coding & keyword use. That's the problem I have: the libraries I need are only available for P 2.7, and I don't know enough to convert them to P 3. :(

1
  • This is the best answer that I have found to this problem.
    – james-see
    Commented May 26, 2016 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.