views:

74

answers:

2

for example the following: str(u'לשום') will throw an error. how can i prevent these?

+4  A: 

Calling str() on a unicode is the same as calling .encode(sys.getdefaultencoding()) on it. If the unicode contains characters that can't be encoded in the default encoding then it will throw a UnicodeEncodeError. The fix is to explicitly encode the unicode in a useful encoding, such as 'utf-8'.

Ignacio Vazquez-Abrams
A: 

If you're running on Python 3, the u'' notation is a syntax error. Is this your problem? Because in Python <3, your code is absolutely correct, and since 'test' is plain ASCII there are no decoding issues.

ΤΖΩΤΖΙΟΥ