python get unicode string size

て烟熏妆下的殇ゞ 提交于 2019-11-27 14:38:12

问题


I have a binary file. This file contains an UTF-8 string. Moreover, it is guaranteed that this string is just a single word. In python, how can I get number of letters in this string?

Let's say, I opened this file and read bytes:

bytes = open("1.dat", "rb").read()

What next have I to do to find out length (in letters, not bytes) of UTF-8 string?


回答1:


unicode_string = bytes.decode("utf-8")
print len(unicode_string)


来源:https://stackoverflow.com/questions/8056496/python-get-unicode-string-size

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