计算 python 中 unicode 字符的字节数
Calculate bytes of the unicode character in python
我正在编写一个 Python 脚本来从文件中读取 Unicode 字符并将它们插入到数据库中。每个字符串我只能插入 30 个字节。
在插入数据库之前如何计算字符串的字节大小?
假设您正在将文件中的 unicode 字符读取到名为 byteString
的变量中。然后您可以执行以下操作:
unicode_string = byteString.decode("utf-8")
print len(unicode_string)
如果您需要知道字节数(文件大小),那么只需调用
bytes_count = os.path.getsize(filename)
.
如果您想了解一个 Unicode 字符可能需要多少字节,则取决于字符编码:
>>> print(u"\N{EURO SIGN}")
€
>>> u"\N{EURO SIGN}".encode('utf-8') # 3 bytes
'\xe2\x82\xac'
>>> u"\N{EURO SIGN}".encode('cp1252') # 1 byte
'\x80'
>>> u"\N{EURO SIGN}".encode('utf-16le') # 2 bytes
'\xac '
要找出一个文件包含多少个 Unicode 字符,您不需要一次读取内存中的整个文件(以防它是一个大文件):
with open(filename, encoding=character_encoding) as file:
unicode_character_count = sum(len(line) for line in file)
如果您在 Python 2 上,则在顶部添加 from io import open
。
相同人类可读文本的确切计数可能取决于 Unicode 规范化(不同的环境可能使用不同的设置):
>>> import unicodedata
>>> print(u"\u212b")
Å
>>> unicodedata.normalize("NFD", u"\u212b") # 2 Unicode codepoints
u'A\u030a'
>>> unicodedata.normalize("NFC", u"\u212b") # 1 Unicode codepoint
u'\xc5'
>>> unicodedata.normalize("NFKD", u"\u212b") # 2 Unicode codepoints
u'A\u030a'
>>> unicodedata.normalize("NFKC", u"\u212b") # 1 Unicode codepoint
u'\xc5'
如示例所示,单个字符 (Å) 可以使用多个 Unicode 代码点表示。
要找出文件中有多少个用户可感知的字符,您可以使用 \X
正则表达式(计算扩展字素簇):
import regex # $ pip install regex
with open(filename, encoding=character_encoding) as file:
character_count = sum(len(regex.findall(r'\X', line)) for line in file)
示例:
>>> import regex
>>> char = u'A\u030a'
>>> print(char)
Å
>>> len(char)
2
>>> regex.findall(r'\X', char)
['Å']
>>> len(regex.findall(r'\X', char))
1
我正在编写一个 Python 脚本来从文件中读取 Unicode 字符并将它们插入到数据库中。每个字符串我只能插入 30 个字节。 在插入数据库之前如何计算字符串的字节大小?
假设您正在将文件中的 unicode 字符读取到名为 byteString
的变量中。然后您可以执行以下操作:
unicode_string = byteString.decode("utf-8")
print len(unicode_string)
如果您需要知道字节数(文件大小),那么只需调用
bytes_count = os.path.getsize(filename)
.
如果您想了解一个 Unicode 字符可能需要多少字节,则取决于字符编码:
>>> print(u"\N{EURO SIGN}")
€
>>> u"\N{EURO SIGN}".encode('utf-8') # 3 bytes
'\xe2\x82\xac'
>>> u"\N{EURO SIGN}".encode('cp1252') # 1 byte
'\x80'
>>> u"\N{EURO SIGN}".encode('utf-16le') # 2 bytes
'\xac '
要找出一个文件包含多少个 Unicode 字符,您不需要一次读取内存中的整个文件(以防它是一个大文件):
with open(filename, encoding=character_encoding) as file:
unicode_character_count = sum(len(line) for line in file)
如果您在 Python 2 上,则在顶部添加 from io import open
。
相同人类可读文本的确切计数可能取决于 Unicode 规范化(不同的环境可能使用不同的设置):
>>> import unicodedata
>>> print(u"\u212b")
Å
>>> unicodedata.normalize("NFD", u"\u212b") # 2 Unicode codepoints
u'A\u030a'
>>> unicodedata.normalize("NFC", u"\u212b") # 1 Unicode codepoint
u'\xc5'
>>> unicodedata.normalize("NFKD", u"\u212b") # 2 Unicode codepoints
u'A\u030a'
>>> unicodedata.normalize("NFKC", u"\u212b") # 1 Unicode codepoint
u'\xc5'
如示例所示,单个字符 (Å) 可以使用多个 Unicode 代码点表示。
要找出文件中有多少个用户可感知的字符,您可以使用 \X
正则表达式(计算扩展字素簇):
import regex # $ pip install regex
with open(filename, encoding=character_encoding) as file:
character_count = sum(len(regex.findall(r'\X', line)) for line in file)
示例:
>>> import regex
>>> char = u'A\u030a'
>>> print(char)
Å
>>> len(char)
2
>>> regex.findall(r'\X', char)
['Å']
>>> len(regex.findall(r'\X', char))
1