如何在 python 中以 unicode 表示形式打印回车符 return?
How to print carriage return in unicode representation in python?
我用的是python2.7,在下面测试
print repr(u'中')
print repr(u'Œ')
print repr(u'Ȧ')
print repr(u'\r')
print repr(u'1')
我得到了结果
u'\u4e2d'
u'\u0152'
u'\u0226'
u'\r'
u'1'
有些以 \u
开头,有些则没有。
如何将 \r
打印为 \u000D
?或 '1'
为 \u0031
?
你需要的是字符的序号,或者它在字符中的位置table。这是由 'ord' 编辑的 return。欲了解更多信息,请参阅:https://docs.python.org/2/library/functions.html#ord and https://docs.python.org/2/howto/unicode.html
回车 return 为 13(请参阅:http://www.theasciicode.com.ar/ascii-control-characters/carriage-return-ascii-code-13.html)ord(u'\r')
(或十六进制的 0xD)
'中'是 20013(请参阅:http://www.unicode.org/cgi-bin/GetUnihanData.pl?codepoint=4E2D)ord(u'中')
如果你想用十六进制表示,你可以做类似 '%04X' % ord(u'中')
这样的事情 return 4E2D
.
希望对您有所帮助。
我用的是python2.7,在下面测试
print repr(u'中')
print repr(u'Œ')
print repr(u'Ȧ')
print repr(u'\r')
print repr(u'1')
我得到了结果
u'\u4e2d'
u'\u0152'
u'\u0226'
u'\r'
u'1'
有些以 \u
开头,有些则没有。
如何将 \r
打印为 \u000D
?或 '1'
为 \u0031
?
你需要的是字符的序号,或者它在字符中的位置table。这是由 'ord' 编辑的 return。欲了解更多信息,请参阅:https://docs.python.org/2/library/functions.html#ord and https://docs.python.org/2/howto/unicode.html
回车 return 为 13(请参阅:http://www.theasciicode.com.ar/ascii-control-characters/carriage-return-ascii-code-13.html)ord(u'\r')
(或十六进制的 0xD)
'中'是 20013(请参阅:http://www.unicode.org/cgi-bin/GetUnihanData.pl?codepoint=4E2D)ord(u'中')
如果你想用十六进制表示,你可以做类似 '%04X' % ord(u'中')
这样的事情 return 4E2D
.
希望对您有所帮助。