3

I am running into a UnicodeEncodeError with a file that I am processing in Python. I have gone through the number of SO entries with similar errors, but none work fix my situation (or do not give sufficient detail to apply them correctly to my problem).

Upon investigating, I've nailed the issue down to the following example:

➜ python
Python 3.5.2 | packaged by conda-forge | (default, Jul  7 2016, 12:51:46)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> y = u'\xe4'
>>> print(y)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character '\xe4' in position 0: ordinal not in range(128)

➜ echo $LANG
en_US.UTF-8

How might I resolve this?

saladi
  • 3,103
  • 6
  • 36
  • 61
  • it'll help if you could mention what end result you need. what kind of resolution you need? one possible resolution is to remove unicode characters from the output. – Kevin Jul 14 '16 at 08:35
  • Under Python 3.5.1 (Anaconda 4.0.0), I don't get any errors running the above code. Both `print(y)` and `str(y)` return ä. Under Python 2.7 I can `print y`, but `str(y)` gives the same error you are getting. Seems a bit odd. – Andrew Guy Jul 14 '16 at 08:50
  • 1
    Try : `print( sys.stdin.encoding)`, `UnicodeEncodeError: 'ascii' codec` which module convert to default encoding `ASCII` . I haven't any information about `conda` but i think a module/build has been changed `idelib` or `using module on idelib`. – dsgdfg Jul 14 '16 at 09:21
  • See my answer here: http://stackoverflow.com/a/35508945/1554386. It's very likely that your locale is broken or not installed. Type `locale` and ensure it returns correctly – Alastair McCormack Jul 18 '16 at 14:55

0 Answers0