繁体   English   中英

为给定的文本块在第n个字符后递归创建一个新的换行符

[英]Recursively create a new line break after nth character for a given block of text

我编写了一个小脚本,该脚本在达到某个字符限制后会创建一个新的换行符。 现在的问题是,脚本从文本末尾开始输出文本。 我似乎无法弄清楚如何以正确的顺序打印文本,而不会使脚本更加复杂。 我正在做一个练习,以更好地理解递归。

这是代码:

def insertNewlines(text, lineLength):

    if len(text) <= lineLength:
        return text
    else:
        return insertNewlines(text[lineLength:], lineLength) + '\n' + text[:lineLength]

这是lineLength为15的测试输出:

length.  
e desired line   
s or exceeds th  
ord that reache  
n' after each w  
e character  '\  
Insert a newlin  
ewriter would.   
e text as a typ  
length, wrap th  
a desired line   
Given text and  

实际输入:

text = "Given text and a desired line length, wrap the text as a typewriter would. Insert a newline character  '\\n' after each word that reaches or exceeds the desired line length."

编辑:根据以下建议修改了代码,以便正确地包装单词:

    if len(text) <= lineLength:
    return text
elif text[lineLength] != ' ':
    return insertNewlines(text[:], lineLength + 1)
else:
    return text[:lineLength] + '\n' + insertNewlines(text[lineLength + 1:], lineLength)

这是新的输出:
给定文本和一个
所需的线长
将文本包装为打字机
将。 插入换行符
每个到达的单词后均带有'\\ n'
或超过所需的线长。

如果您不希望以最大宽度textwrap ,请尝试使用textwrap库,请参阅http://docs.python.org/2/library/textwrap.html

from textwrap import TextWrapper

text = "Given text and a desired line length, wrap the text as a typewriter would. Insert a newline character  '\\n' after each word that reaches or exceeds the desired line length."

tw = TextWrapper()
tw.width = 20

print "\n".join(tw.wrap(text))

[出]:

Given text and a
desired line length,
wrap the text as a
typewriter would.
Insert a newline
character  '\n'
after each word that
reaches or exceeds
the desired line
length.

这是一个本地python实现:

text = "Given text and a desired line length, wrap the text as a typewriter would. Insert a newline character  '\\n' after each word that reaches or exceeds the desired line length."

def wrap(txt, width):
    tmp = ""
    for i in txt.split():
        if len(tmp) + len(i) < width:
            tmp+=" "+i
        else:
            print tmp.strip()
            tmp = i

wrap(text, 20)

一种更pythonic的yield方法:

def wrap(txt, width):
    tmp = ""
    for i in txt.split():
        if len(tmp) + len(i) < width:
            tmp+=" "+i
        else:
            yield tmp.strip()
            tmp = i

print "\n".join(i for i in wrap(text, 20))

问题在于递归调用的顺序。 它应该位于功能的末尾,以实现您想要的功能。 尝试这个:

def insertNewlines(text, lineLength):

    if len(text) <= lineLength:
        return text
    else:
        return text[:lineLength] + '\n' + insertNewlines(text[lineLength:], lineLength)

text = "Given text and a desired line length, wrap the text as a typewriter would. Insert a newline character  '\\n' after each word that reaches or exceeds the desired line length."
print insertNewlines(text, 15)

输出:

Given text and 
a desired line 
length, wrap th
e text as a typ
ewriter would. 
Insert a newlin
e character  '\
n' after each w
ord that reache
s or exceeds th
e desired line 
length.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM