Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python get unicode string size

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?

like image 800
Nick Avatar asked Nov 08 '11 20:11

Nick


1 Answers

unicode_string = bytes.decode("utf-8")
print len(unicode_string)
like image 70
Håvard Avatar answered Oct 19 '22 23:10

Håvard