首页 \ 问答 \ 在Python中包装多行字符串(保留现有的换行符)?(Wrap multiline string (preserving existing linebreaks) in Python?)

在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
更新时间:2020-11-23 21:11

最满意答案

找到了。 你用'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".
  • 这适用于反向复制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 ...
  • 我并不完全惊讶,你的例子没有表现出奇怪的行为。 尝试将str1复制到str1+2 ,看看会发生什么。 (实际上可能没有区别,取决于编译器/库)。 一般来说,memcpy是以简单(但是快速的方式)实现的。 简单来说,它只是循环数据(按顺序),从一个位置复制到另一个位置。 这可能会导致源被读取时被覆盖。 Memmove做更多的工作,以确保它正确处理重叠。 编辑: (不幸的是,我找不到体面的例子,但这些会做)。 对比此处显示的memcpy和memmove实现。 memcpy只是循环,而memmove执行测试以确定 ...
  • 因为需要通知对象它正被移动。 例如,可能存在指向需要修复的给定对象的指针,因为正在复制对象。 或者可能需要更新引用计数智能指针的引用计数。 要么.... 如果你只是记忆了底层内存,那么你最终会在同一个对象上调用两次析构函数,这也很糟糕。 如果析构函数控制类似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 ...
  • 您的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( ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)