在Python中,是否有一种方法可以插入换行符并将每68个字符的字符串空格?

时间:2015-01-15 17:26:57

标签: python string python-2.7 insert

我正在阅读描述并将它们转换为python中的字符串。由于标准过时,每行的长度不得超过80个字符。前12个是&#39 ;;'接着是11个空格,最后68个应该是字符串。

我们有超过300个字符的字符串,所以我需要每68个字符插入一个'\n;'和11个空格。我们还有一些字符串已经包含需要维护的'\n'。如果单词没有被分解,也会优先考虑。

这在python中是否可行?

我试图这样做,但没有运气。

def badChars(stri):
    p=0
    if len(stri) > 68:
        for i in range(0, len(stri)):
            if stri[i] == "\\" and stri[i+1] == "n":
                p=-1
            if p > 68:
                stri[i] = stri[i] + "\n;                 "
                p=-1
            p=p+1  

我需要保持原封不动的字样并保持当前的\n

1 个答案:

答案 0 :(得分:3)

您可以使用textwrap module换行;您也可以指定要使用的缩进:

import textwrap

indentation = ';' + ' ' * 11
result = textwrap.fill(
    input_text, width=80,
    initial_indent=indentation, subsequent_indent=indentation)

如果您需要维护现有换行符,请首先拆分换行符,对每行应用换行,然后重新加入换行符:

indentation = ';' + ' ' * 11
params = {
    'width': 80,
    'initial_indent': indentation,
    'subsequent_indent': indentation
}
result = '\n'.join([textwrap.fill(line, **params)
                    for line in input_text.splitlines()])

str.splitlines() method是按行分隔符分割行的最简单方法。

演示:

>>> import textwrap
>>> input_text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
>>> indentation = ';' + ' ' * 11
>>> result = textwrap.fill(
...     input_text, width=80,
...     initial_indent=indentation, subsequent_indent=indentation)
>>> print result
;           Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
;           eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
;           ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
;           aliquip ex ea commodo consequat. Duis aute irure dolor in
;           reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
;           pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
;           culpa qui officia deserunt mollit anim id est laborum.
>>>
>>> # produce a sample with newlines by inserting these around 170 characters
... 
>>> input_text = textwrap.fill(input_text, width=170)
>>> print input_text
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
>>> params = {
...     'width': 80,
...     'initial_indent': indentation,
...     'subsequent_indent': indentation
... }
>>> result = '\n'.join([textwrap.fill(line, **params)
...                     for line in input_text.splitlines()])
>>> print result
;           Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
;           eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
;           ad minim veniam, quis nostrud
;           exercitation ullamco laboris nisi ut aliquip ex ea commodo
;           consequat. Duis aute irure dolor in reprehenderit in voluptate velit
;           esse cillum dolore eu fugiat nulla
;           pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
;           culpa qui officia deserunt mollit anim id est laborum.

指定的宽度包括缩进。请注意,它将使用空格来包装整个单词的行。有关控制线条包装方式的更多选项,请参阅模块文档。