Unicode, Printing to stdout, what is happening?

I have problems with printing data to stdout. I understand the concept of Unicode and Ascii, but I fail to see why a printing instruction doesn't work.

I'm playing with data from a JSON file of a computer game, Fallout Shelter.

when I try to print the data with :

   for i in jsondata["dwellers"]["dwellers"]:
        print "{},{} {},{}".format(f,i["name"],i["lastName"],i["relations"])

I get an error :

Traceback (most recent call last):
  File "f:\FOSScript\Tree.py", line 81, in <module>
    tree()
  File "f:\FOSScript\Tree.py", line 76, in tree
    graphing(jsondata)
  File "f:\FOSScript\Tree.py", line 35, in graphing
    print "{} {}".format(i["name"],i["lastName"])
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 3: ordinal not in range(128)

The u'\xe9' is an french accent.

But in the code, when I do :

prenom = u'Val\xe9rie'
print prenom

The name is printed correctly.

The following instructions :

print locale.getpreferredencoding()
print sys.stdout.encoding

print the following:

cp1252
cp850

Both Code pages (as checked on Wikipedia) contain the accent characters.

I am on Windows 10, a French-Canada version. Python 2.7.13.

==========================

The only supported default encodings in Python are:

Python 2.x: ASCII
Python 3.x: UTF-8

When they say ASCII, do they mean an extended ASCII with accents, since the JSON data contains the accents, python is just "unable" to print it?

Thank you!

Upvotes: 0

Views: 97

Answers (1)

wphicks
wphicks

Reputation: 355

The issue is that you are trying to use string formatting on a non-unicode string with unicode characters. Instead you want:

for i in jsondata["dwellers"]["dwellers"]:
        print u"{},{} {},{}".format(f,i["name"],i["lastName"],i["relations"])

Notice the extra u before the quote in the second line.

Testing this on python 2.7

print u"{}".format(u'\xe9')

works fine.

Upvotes: 1

Related Questions