簡體   English   中英

Python Unicode打印錯誤

[英]Python Unicode Print Error

做一些unicode測試,遇到一個我無法克服的錯誤。

# -- 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()

很好 但是,當我嘗試使用標記為#1而不是數字#2的打印行進行打印時,出現此錯誤:

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

我研究了該錯誤,但似乎與我的格式有關。 但是,格式在#1中與#2中相同或非常相似。

任何幫助,將不勝感激。 謝謝。

將格式轉換為單極字符串。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM