python新手,我的程序需要一些帮助。我有一个代码,它接收一个未格式化的文本文档,进行一些格式化(设置页宽和边距),然后输出一个新的文本文档。我的整个代码工作正常,除了这个产生最终输出的函数。
这是问题代码的一部分:
def process(document, pagewidth, margins, formats):
res = []
onlypw = []
pwmarg = []
count = 0
marg = 0
for segment in margins:
for i in range(count, segment[0]):
res.append(document[i])
text = ''
foundmargin = -1
for i in range(segment[0], segment[1]+1):
marg = segment[2]
text = text + '\n' + document[i].strip(' ')
words = text.split()
注意:segment [0] 表示文档的开头,如果您想知道范围,segment[1] 仅表示文档的结尾。我的问题是当我将文本复制到单词时(在 words=text.split() 中)它不会保留我的空行。我应该得到的输出是:
This is my substitute for pistol and ball. With a
philosophical flourish Cato throws himself upon his sword; I
quietly take to the ship. There is nothing surprising in
this. If they but knew it, almost all men in their degree,
some time or other, cherish very nearly the same feelings
towards the ocean with me.
There now is your insular city of the Manhattoes, belted
round by wharves as Indian isles by coral reefs--commerce
surrounds it with her surf.
我当前的输出是什么样的:
This is my substitute for pistol and ball. With a
philosophical flourish Cato throws himself upon his sword; I
quietly take to the ship. There is nothing surprising in
this. If they but knew it, almost all men in their degree,
some time or other, cherish very nearly the same feelings
towards the ocean with me. There now is your insular city of
the Manhattoes, belted round by wharves as Indian isles by
coral reefs--commerce surrounds it with her surf.
我知道当我将文本复制到单词时会出现问题,因为它不会保留空白行。我怎样才能确保它复制空行加上单词?如果我应该添加更多代码或更详细信息,请告诉我!