I have a script which shows
UnicodeEncodeError: 'ascii' codec can't encode character u'\u044e' in position 0: ordinal not in range(128)
for line print ord(u), u
. How can I run the script ok?
I run C:\\Python27\\pythonw.exe name.py
.
# -*- encoding: utf-8 -*-
print "Russian letters".center(18*4)
i = 0
for c in "АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ"\
"абвгдежзийклмнопрстуфхцчшщъыьэюя":
u = unicode(c, 'koi8-r')
print ord(u), u
i += 1
if i % 4 == 0:
print
You cannot print a unicode charater to the windows console if the local encoding doesn't support it. So python tries to encode it as ascii
and fails. The problematic line is:
print ord(u), u
u
is a unicode character. What you want is probably:
# -*- encoding: utf-8 -*-
print "Russian letters".center(18*4)
i = 0
for c in u"АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ"\
u"абвгдежзийклмнопрстуфхцчшщъыьэюя":
u = c.encode('koi8-r')
print ord(u), u
i += 1
if i % 4 == 0:
print
Also use python.exe
for console applications, not pythonw.exe
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.