在Python中包装多行字符串(保留现有的换行符)?(Wrap multiline string (preserving existing linebreaks) in Python?)
考虑这个例子:
import textwrap import pprint mystr=r""" First line. Second line. The third line is a very long line, which I would like to somehow wrap; wrap at 80 characters - or less, or more! ... can it really be done ??""" pprint.pprint(textwrap.wrap(mystr,80))
字符串
mystr
已经是一个多行字符串,因为它包含换行符; 但是,如果我运行此脚本,我得到输出:[' First line. Second line. The third line is a very long line, which I would like', 'to somehow wrap; wrap at 80 characters - or less, or more! ... can it really be', 'done ??']
...这意味着
textwrap.wrap
首先“加入”多行字符串(即删除其中的现有换行符),然后才将其包装(即将其拆分为给定的字符数)。如何包装多行字符串,以便保留换行符? 也就是说,在这种情况下,预期输出将是:
['First line.', 'Second line.', 'The third line is a very long line, which I would like to somehow wrap; wrap at', '80 characters - or less, or more! ... can it really be done ??']
编辑; 感谢@u_mulder的评论,我试过:
textwrap.wrap(mystr,80,replace_whitespace=False)
我得到了:
['\nFirst line.\nSecond line.\nThe third line is a very long line, which I would like', 'to somehow wrap; wrap at 80 characters - or less, or more! ... can it really be', 'done ??']
换行似乎被保留,但作为“内联”字符; 所以这里第一个元素本身就是一个多行字符串 - 所以它不是我需要的,每一行都是一个数组元素。
Consider this example:
import textwrap import pprint mystr=r""" First line. Second line. The third line is a very long line, which I would like to somehow wrap; wrap at 80 characters - or less, or more! ... can it really be done ??""" pprint.pprint(textwrap.wrap(mystr,80))
The string
mystr
is already a multiline string, given that it contains linebreaks; however, if I run this script, I get as output:[' First line. Second line. The third line is a very long line, which I would like', 'to somehow wrap; wrap at 80 characters - or less, or more! ... can it really be', 'done ??']
... which means that
textwrap.wrap
first "joined" the multiline string (that is, removed the existing linebreaks in it), and only then wrapped it (i.e. split it at the given number of characters).How can I wrap a multiline string, such that the line feeds are preserved? that is, in this case, the expected output would be:
['First line.', 'Second line.', 'The third line is a very long line, which I would like to somehow wrap; wrap at', '80 characters - or less, or more! ... can it really be done ??']
EDIT; thanks to comment by @u_mulder, I tried:
textwrap.wrap(mystr,80,replace_whitespace=False)
and with that I get:
['\nFirst line.\nSecond line.\nThe third line is a very long line, which I would like', 'to somehow wrap; wrap at 80 characters - or less, or more! ... can it really be', 'done ??']
The line breaks seem to be preserved, but as "inline" characters; so here the first element is a multiline string in itself -- and so it is not as I require it, with every line as an array element.
原文:https://stackoverflow.com/questions/28863889
最满意答案
找到了。 你用'H'覆盖标签的第一个字节。 其他字节都很好。 现在找出H来自哪里......
nextLoc = nextLoc + sizeof(sizeType) + sizeof(unsigned char) + idSize*sizeof(char) + value.getSize();
你需要在这里再添加一个。 你有skip(sizeType),长度字节(unsigned char),id本身(idSize * sizeof(char))和值(value.getSize()),但是你还要在id和value之间留一个字节不考虑。 这就是为什么你的标签的最后一个字节被覆盖 - 并且因为你在一个小端机器上测试导致最高字节被破坏。
for(int i = 0; i < *((unsigned char*)idSize); ++i){ ... tbl_char_ptr = tbl_char_ptr + sizeof(char); ... } result_ptr = tbl_char_ptr + sizeof(char);
这比idSize多一个。
Found it. You're overwriting the first byte of your tag with an 'H'. The other bytes are fine. Now to find where that H is coming from...
nextLoc = nextLoc + sizeof(sizeType) + sizeof(unsigned char) + idSize*sizeof(char) + value.getSize();
You need to add one more here. You have the skip (sizeType), the length byte (unsigned char), the id itself (idSize * sizeof(char)) and the value (value.getSize()), but you also leave a byte between id and value that you're not accounting for. That's why the last byte of your tag is getting overwritten - and because you're testing on a little-endian machine that results in the highest byte being corrupted.
for(int i = 0; i < *((unsigned char*)idSize); ++i){ ... tbl_char_ptr = tbl_char_ptr + sizeof(char); ... } result_ptr = tbl_char_ptr + sizeof(char);
That's one more than idSize.
相关问答
更多-
我可以防止由std::memcpy复制对象吗? 简单回答是不”。 Can I prevent object from being copied by std::memcpy? The simple answer is "No".
-
C memcpy相反(C memcpy in reverse)[2022-08-21]
这适用于反向复制int s: void reverse_intcpy(int *restrict dst, const int *restrict src, size_t n) { size_t i; for (i=0; i < n; ++i) dst[n-1-i] = src[i]; } 就像memcpy() , dst和src指向的区域不能重叠。 如果你想在原地逆转: void reverse_ints(int *data, size_t n) { size ... -
在这种情况下,我找到了一种提高速度的方法。 我写了一个多线程版本的memcpy,拆分要在线程之间复制的区域。 以下是使用与上述相同的时间码,设置块大小的一些性能缩放数字。 我不知道性能,特别是对于这个小尺寸的块,可以扩展到这么多线程。 我怀疑这与本机上大量的内存控制器(16)有关。 Performance (10000x 4MB block memcpy): 1 thread : 1826 MB/sec 2 threads: 3118 MB/sec 3 threads: 4121 MB/sec ...
-
memcpy()vs memmove()(memcpy() vs memmove())[2022-05-21]
我并不完全惊讶,你的例子没有表现出奇怪的行为。 尝试将str1复制到str1+2 ,看看会发生什么。 (实际上可能没有区别,取决于编译器/库)。 一般来说,memcpy是以简单(但是快速的方式)实现的。 简单来说,它只是循环数据(按顺序),从一个位置复制到另一个位置。 这可能会导致源被读取时被覆盖。 Memmove做更多的工作,以确保它正确处理重叠。 编辑: (不幸的是,我找不到体面的例子,但这些会做)。 对比此处显示的memcpy和memmove实现。 memcpy只是循环,而memmove执行测试以确定 ... -
在STL中使用memcpy(Using memcpy in the STL)[2024-01-13]
因为需要通知对象它正被移动。 例如,可能存在指向需要修复的给定对象的指针,因为正在复制对象。 或者可能需要更新引用计数智能指针的引用计数。 要么.... 如果你只是记忆了底层内存,那么你最终会在同一个对象上调用两次析构函数,这也很糟糕。 如果析构函数控制类似OS文件句柄的内容怎么办? 编辑:总结以上内容:复制构造函数和析构函数可能有副作用。 这些副作用需要保留。 Because the object needs to be notified that it is being moved. For examp ... -
首先是一个大警告: “不安全必须死” http://blog.takipi.com/still-unsafe-the-major-bug-in-java-6-that-turned-into-a-java-9-feature/ 一些先决条件 static class DataHolder { int i1; int i2; int i3; DataHolder d1; DataHolder d2; public DataHolder(int i1, int i ...
-
std::string用于字符串。 如果你想要一个字节缓冲区,你应该使用std::vector
(或其有符号/无符号的对应)。 使用std::copy几乎总是要走的路,特别是在所有类的“高级C ++领域”中。 不过,我会说当你处理像字节缓冲区这样的低级结构时,C ++函数std::memcpy是最合适的选择。 请记住, std::memcpy是一个只能复制字节的“哑”函数,但考虑到我们试图填充一个字节缓冲区,这就是我们想要的。 std::vector buffer(32); uint ... -
堆栈在memcpy之后损坏了(stack corrupted after memcpy)[2022-07-27]
您的dataholder变量是一个指向backditup大小的数组的指针,而不是数组本身。 因此,当你进行Zeromemory和memcpy调用时,你不应该使用它的地址; 相反,写: ZeroMemory(dataholder,sizeof(backditup)); memcpy(dataholder,&backditup,sizeof(backditup)); 没有& 。 同样,当您复制数据时,您需要: memcpy(&restorenaarhier,dataholder,sizeof(restoren ... -
你的意思是为一些任意的C ++类型T吗? 除非您知道T是POD(普通旧数据,基本上是C结构)类型,否则使用memcpy复制类型为T对象是不安全的。 例如,这会阻止T的拷贝构造函数运行,这可能导致不正确的拷贝(例如,尝试memcpy std::vector不会复制数据缓冲区)。 Do you mean working for some arbitrary C++ type T? Unless you know that T is a POD (plain old data, basically a C str ...
-
找到了。 你用'H'覆盖标签的第一个字节。 其他字节都很好。 现在找出H来自哪里...... nextLoc = nextLoc + sizeof(sizeType) + sizeof(unsigned char) + idSize*sizeof(char) + value.getSize(); 你需要在这里再添加一个。 你有skip(sizeType),长度字节(unsigned char),id本身(idSize * sizeof(char))和值(value.getSize( ...