1

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.

1
  • Why are you not using unichr() instead? Commented Jun 2, 2015 at 3:25

1 Answer 1

2

Convert formatting pattern into uniode string.

print(u"{} | {:04x} | {}".format(x, ord(x), name(x,'-')))
Sign up to request clarification or add additional context in comments.

1 Comment

That was the ticket. You the dude. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.