0

I am calling some sort of API which returns an object with three properties:

obj.name
obj.id
obj.text

all of these could come in different encoding.

In order to output it correctly to my terminal, I am now doing

print obj.name.encode('UTF-8')
print obj.id.encode('UTF-8')
print obj.text.encode('UTF-8')

Is there a way I can simply do one time to set my default encoding to UTF-8?

I read some other post which suggested calling the sys.setdefaultencoding('UTF-8'), which is not available anymore in python 2.7.x

Please advise.

tuxtimo
  • 2,730
  • 20
  • 29
Cloud Yoda
  • 33
  • 1
  • 4

1 Answers1

3

Normally Python detects the output encoding from the terminal settings; you can see what Python detected by running:

import sys
sys.stdout.encoding

You really want to configure your terminal to specify the correct encoding if that doesn't work for you; set the LC_CTYPE environment variable on POSIX systems for example.

You can also force Python to use an encoding by setting the PYTHONIOENCODING environment variable:

$ PYTHONIOENCODING=latin-1 python -c 'import sys; print sys.stdout.encoding'
latin-1
$  PYTHONIOENCODING=utf8 python -c 'import sys; print sys.stdout.encoding'
utf8
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • It throws error if I did not specifically set the encoding to UTF-8. And my terminal has been set to in the .bash_profile: export LANG="en_US.UTF-8". I think my question is more about how can we set the global environment of a python script to a particular encoding rather than having to set each line individually during the print out. – Cloud Yoda Feb 19 '13 at 14:38
  • @CloudYoda: That's what `PYTHONIOENCODING` does. If your bash profile already sets `LANG` then Python *should* already detect your terminal encoding, unless you are redirecting the script output with a pipe or `>` shell stream redirection. – Martijn Pieters Feb 19 '13 at 14:46
  • Thanks! It seems to work...although it throw different exception in my IDE..but i think thats due to my env setting with it. – Cloud Yoda Feb 19 '13 at 15:15
  • @CloudYoda: Right, if you are running Python in an IDE, then the stdout encoding detected will be different from your terminal again! – Martijn Pieters Feb 19 '13 at 15:18