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.

0

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

4 Comments

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.
@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.
Thanks! It seems to work...although it throw different exception in my IDE..but i think thats due to my env setting with it.
@CloudYoda: Right, if you are running Python in an IDE, then the stdout encoding detected will be different from your terminal again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.