JayJay123
JayJay123

Reputation: 323

Python Unicode Print Error

Doing some unicode testing and have encountered an error that I have not been able to overcome.

# -- coding: utf-8 -- Enable direct Unicade-8 encoding
    # Imports #
from __future__ import print_function
import locale
from unicodedata import *

locale.setlocale(locale.LC_ALL, '') # Set the locale for your system 'en_US.UTF-8'

def main():
    xlist=[]
    for i in range(9729, 9731):
        xlist.append(eval('u"\\u{:04x}"'.format(i)))
    for x in xlist:
        #print(name(u' ','-'))
        if name(x,'-')!='-':
            #print("{} | {:04x} | {}".format(x, ord(x), name(x,'-'))) #1

            print( x,'|', "%04x"%(ord(x)), '|', name(x,'-')) #2

if __name__ == '__main__':
    main()

That runs fine. But when I change to trying to print using the print line labeled #1 instead of number #2, I get this error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2601' in position 0: ordinal not in range(128)

I have researched the error, but it seems to be with my formatting. However, the formatting is the same, or very similar, in #1 as in #2.

Any help would be appreciated. Thanks.

Upvotes: 1

Views: 98

Answers (1)

Konstantin
Konstantin

Reputation: 25389

Convert formatting pattern into uniode string.

print(u"{} | {:04x} | {}".format(x, ord(x), name(x,'-')))

Upvotes: 2

Related Questions