简体   繁体   English

如何在Python中打印Unicode字符代码?

[英]How does one print a Unicode character code in Python?

I would like to print a unicode's character code, and not the actual glyph it represents in Python. 我想打印一个unicode的字符代码,而不是它在Python中表示的实际字形。

For example, if u is a list of unicode characters: 例如,如果u是unicode字符列表:

>>> u[0]
u'\u0103'
>>> print u[0]
ă

I would like to output the character code as a raw string: u'\ă' . 我想将字符代码输出为原始字符串: u'\ă'

I have tried to just print it to a file, but this doesn't work without encoding it in UTF-8 . 我试图将它打印到一个文件,但如果没有在UTF-8编码它,这是行不通的。

>>> w = open('~/foo.txt', 'w')
>>> print>>w, u[0].decode('utf-8')

Traceback (most recent call last):
  File "<pyshell#33>", line 1, in <module>
    print>>w, u[0].decode('utf-8')
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0103' in position 0: ordinal not in range(128)
>>> print>>w, u[0].encode('utf-8')
>>> w.close()

Encoding it results in the glyph ă being written to the file. 编码它导致字形ă被写入文件。

How can I write the character code? 我怎么写字符代码?

For printing raw unicode data one only need specify the correct encoding: 要打印原始unicode数据,只需指定正确的编码:

>>> s = u'\u0103'
>>> print s.encode('raw_unicode_escape')
\u0103

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM