有没有一种Python方法可以将Unicode字符串截断最大字节数



如果API接受某个限制字节数的字符串值,但接受Unicode,是否有更好的方法来缩短有效Unicode的字符串?

def truncate(string: str, length: int):
"""Shorten an Unicode string to a certain length of bytes."""
if len(string.encode()) <= length:
return string
chars = list(string)
while sum(len(char.encode()) for char in chars) > length:
chars.pop(-1)
return "".join(chars)

这应该在Python-3:中工作

bytes_ = string.encode()
try:
return bytes_[:length].decode()
except UnicodeDecodeError as err:
return bytes_[:err.start].decode()

基本上,我们在第一个解码错误时截断。UTF-8是一个前缀代码。因此,解码器应该总是能够看到字符串何时在字符中间被截断。口音和其他东西可能会引起怪异。我还没想清楚。也许我们也需要一些正常化。

在Python-2中,请确保指定编码。

最新更新