【发布时间】:2010-02-05 09:36:03
【问题描述】:
如何在 Python 中将字符串打印为 unicode 代码序列?
输入:"если"(俄语)。
输出:"\u0435\u0441\u043b\u0438"
【问题讨论】:
如何在 Python 中将字符串打印为 unicode 代码序列?
输入:"если"(俄语)。
输出:"\u0435\u0441\u043b\u0438"
【问题讨论】:
这应该可行:
>>> s = u'если'
>>> print repr(s)
u'\u0435\u0441\u043b\u0438'
【讨论】:
代码:
txt = u"если"
print repr(txt)
输出:
u'\u0435\u0441\u043b\u0438'
【讨论】:
a = u"\u0435\u0441\u043b\u0438"
print "".join("\u{0:04x}".format(ord(c)) for c in a)
【讨论】:
如果你需要特定的编码,你可以使用:
txt = u'если'
print txt.encode('utf8')
print txt.encode('utf16')
【讨论】: