How to handle multibyte string in Python

為{幸葍}努か 提交于 2019-12-01 05:57:13

Use Unicode strings:

# Encoding: UTF-8

japanese = u"桜の花びらたち"
print japanese
print len(japanese)

Note the u in front of the string.

To convert a bytestring into Unicode, use decode: "桜の花びらたち".decode('utf-8')

Try converting it to unicode first:

print len(japanese.decode("utf-8"))

gives 7. You are working on the utf-8 encoded string, which indeed has 21 bytes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!