gpt4 book ai didi

python - 使用带字节数的 textwrap.wrap

转载 作者:太空狗 更新时间:2023-10-30 01:27:53 26 4
gpt4 key购买 nike

如何使用 textwrap 模块在一行达到一定字节数之前进行拆分(不拆分多字节字符)?

我想要这样的东西:

>>> textwrap.wrap('☺ ☺☺ ☺☺ ☺ ☺ ☺☺ ☺☺', bytewidth=10)
☺ ☺☺
☺☺ ☺
☺ ☺☺
☺☺

最佳答案

结果取决于使用的编码,因为每个字节数字符是编码的函数,在许多编码中,性格也是如此。我假设我们使用的是 UTF-8,其中 '☺' 是编码为 e298ba 并且是三个字节长;给定的例子是与该假设一致。

textwrap 中的所有内容都适用于字符;它什么都不知道关于编码。解决此问题的一种方法是将输入字符串转换为另一种格式,每个字符变成一串字符其长度与字节长度成正比。我会用三个字符:两个十六进制字节,加上一个控制换行符。因此:

'a' -> '61x'         non-breaking
' ' -> '20 ' breaking
'☺' -> 'e2x98xbax' non-breaking

为简单起见,我假设我们只在空格处打断,而不是制表符或任何其他角色。

import textwrap

def wrapbytes(s, bytewidth, encoding='utf-8', show_work=False):
byts = s.encode(encoding)
encoded = ''.join('{:02x}{}'.format(b, ' ' if b in b' ' else 'x')
for b in byts)
if show_work:
print('encoded = {}\n'.format(encoded))
ewidth = bytewidth * 3 + 2
elist = textwrap.wrap(encoded, width=ewidth)
if show_work:
print('elist = {}\n'.format(elist))
# Remove trailing encoded spaces.
elist = [s[:-2] if s[-2:] == '20' else s for s in elist]
if show_work:
print('elist = {}\n'.format(elist))
# Decode. Method 1: inefficient and lengthy, but readable.
bl1 = []
for s in elist:
bstr = "b'"
for i in range(0, len(s), 3):
hexchars = s[i:i+2]
b = r'\x' + hexchars
bstr += b
bstr += "'"
bl1.append(eval(bstr))
# Method 2: equivalent, efficient, terse, hard to read.
bl2 = [eval("b'{}'".format(''.join(r'\x{}'.format(s[i:i+2])
for i in range(0, len(s), 3))))
for s in elist]
assert(bl1 == bl2)
if show_work:
print('bl1 = {}\n'.format(bl1))
dlist = [b.decode(encoding) for b in bl1]
if show_work:
print('dlist = {}\n'.format(dlist))
return(dlist)

result = wrapbytes('☺ ☺☺ ☺☺ ☺ ☺ ☺☺ ☺☺', bytewidth=10, show_work=True)
print('\n'.join(result))

关于python - 使用带字节数的 textwrap.wrap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36244817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com