python获取unicode字符串大小 [英] python get unicode string size

查看:278
本文介绍了python获取unicode字符串大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二进制文件.该文件包含一个UTF-8字符串.此外,可以确保此字符串只是一个单词. 在python中,如何获取此字符串中的字母数?

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()

接下来我该怎么做才能找出UTF-8字符串的长度(以字母而不是字节为单位)?

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

推荐答案

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

这篇关于python获取unicode字符串大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆