- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如何使用 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/
为什么是textwrap.wrap()和 textwrap.fill()太慢了?例如,在我的笔记本电脑上包装一个 10000 个字符的字符串需要将近两秒半。 $ python -m timeit -n
>>> import textwrap >>> string = "This is a very very very very very long string." >>> print textwra
我发现 python 的 textwrap 库在错误的地方断句。我正在使用: wrp = textwrap.TextWrapper(width=32,break_long_words=False,re
这里完全是编码初学者,第一次在这个好地方发帖。使用 Python 3.2.3。 简要说明 我想对字符串进行自动换行和字符填充,但应该忽略字符串的某些部分。 背景 我正在使用一个从 .txt 文件输出文
我在将文本包装在堆叠的列和行中时遇到了一些麻烦(而且我不太确定我是否在做良好的练习;))。我尝试使用 Flexible 而不是 Wrapped,但它也没有用。 代码: ListView(childre
在 Windows Phone 7 应用程序中,当我在网格中放置一个 TextBlock 并设置它的 HorizontalAlignment 时到“Center”和它的TextWrapping对于“W
当我将 TextBlock 放置在水平对齐的 StackPanel 内时,它不会换行。我意识到这是因为 StackPanel 的可用宽度是 PositiveInfinity,但是有什么解决方法吗? 我
目标 我有一个 WPF 应用程序,它在 DataGrid 中包含文本。我希望文本包装在每个单元格内。我还想为每一列设置一个 MinWidth,如果必须,ScrollViewer 应该创建 horizo
如何使用 textwrap 模块在一行达到一定字节数之前进行拆分(不拆分多字节字符)? 我想要这样的东西: >>> textwrap.wrap('☺ ☺☺ ☺☺ ☺ ☺ ☺☺ ☺☺', bytewid
在 WPF 窗口中拖放一些控件(如 TextBox)后,您可以在 XAML 中看到下面的行 这些属性会自动插入到 XAML 中!只是在 XAML 中插入了一些特殊属性。 问题: 为什么只有这些特殊属
我有一个 UserControl,它有几个按钮和一些 Textblock。出于某种原因,TextWrap 不适用于此文本 block 。
在使用 Python 的 textwrap 库时,如何将其转换为: short line, long line xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
我不确定为什么我的代码没有正确执行 TextWrapping。它不会包装 Description 列的文本(这是我想要的)。它只是将其切断,甚至不使用“...”来让我知道有更多数据。 我尝试使用我在网
好的...所以this解决方案没有帮助 XAML 在这里
我有以下组合框 我希望将文本换行应用于此组合框,并遵循答案 here 中的代码片段 但是这个模板破坏了绑定(bi
我正在尝试使用 textwrap 来格式化导入文件,该文件的格式化方式非常特殊。基本上如下(为简单起见,缩短了行长): abcdef <- Ok line abcdef ghijk <- Note
我正在动态生成一个 DataGrid 并将其添加到我的 WPF 应用程序的 StackPanel 中。 由于是动态生成的,因此在 XAML 端没有相同的标记,我需要以编程方式管理绑定(bind)和所有
我在 Windows Phone 中遇到了一个看似简单的布局问题。问题如下图所示: 我的要求如下: 副标题必须始终可见,但要以标题文本溢出为代价。 标题和副标题必须垂直和水平居中。 当我使用 Stac
所以我只是重新格式化了一堆代码以合并 textwrap.wrap,却发现我所有的\n 都不见了。这是一个例子。 from textwrap import wrap def wrapAndPrint (
我有一个文本,由几个段落组成,段落之间用双换行符分隔。我想将它们格式化为 70 行宽,保留新行,整个内容应该以一个非缩进行开始,并带有 Abstract: Lorem ipsum ...。 所以整个事
我是一名优秀的程序员,十分优秀!