使用python将文本解析为段落(循环问题)

使用python将文本解析为段落(循环问题),python,loops,Python,Loops,我正在使用GoogleSheetsAPI和python从电子表格中输入的数据生成HTML标记。有时用户在单个单元格中输入长文本块,我希望在出现新行时使用python将其解析为语义段落 使用str.splitlines和forloop,我可以让它在概念上工作,但是循环的第一次迭代已经打印出来了 !/usr/bin/python 电子表格中的示例文本 text=Lorem Ipsum只是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是行业标准的虚拟文本,当时一位不知名的印刷商拿

我正在使用GoogleSheetsAPI和python从电子表格中输入的数据生成HTML标记。有时用户在单个单元格中输入长文本块,我希望在出现新行时使用python将其解析为语义段落

使用str.splitlines和forloop,我可以让它在概念上工作,但是循环的第一次迭代已经打印出来了

!/usr/bin/python 电子表格中的示例文本 text=Lorem Ipsum只是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是行业标准的虚拟文本,当时一位不知名的印刷商拿起一个打印工具,将其拼凑成一本打印样本书。它不仅存活了五个世纪,而且还跨越到电子排版,基本上保持不变。 它在20世纪60年代随着包含Lorem Ipsum段落的Letraset表单的发布而流行,最近随着Aldus PageMaker等桌面出版软件的发布,包括Lorem Ipsum版本。 将介绍文本分成段落 def pgparsetext: 在每一条新的线路上分开 lines=text.splitlines 在p标记中换行 对于行中的i: 返回''+i+'

' printpgparsetext 结果:

<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
预期结果:

<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
<p>It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

你只回了第一行。你的第二行从来没有包装过。 试试这个:

#!/usr/bin/python

#sample text from spreadsheet
text = """Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."""

#break intro text into paragraphs
def pgparse(text):
    #split at every new line
    lines = text.splitlines()
    #wrap lines in p tags
    return "\n".join('<p>'+i+'</p>' for i in lines)

print(pgparse(text))

使用生成器表达式包装行,然后用\n

将它们重新连接,您只返回第一行。你的第二行从来没有包装过。 试试这个:

#!/usr/bin/python

#sample text from spreadsheet
text = """Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."""

#break intro text into paragraphs
def pgparse(text):
    #split at every new line
    lines = text.splitlines()
    #wrap lines in p tags
    return "\n".join('<p>'+i+'</p>' for i in lines)

print(pgparse(text))
使用生成器表达式包装行,然后使用

此行退出函数。也许你想要:

def pgparsetext: 结果=[] 在每一条新的线路上分开 lines=text.splitlines 在p标记中换行 对于行中的i: 结果。附加“”+i+”

” 返回结果 此行退出函数。也许你想要:

def pgparsetext: 结果=[] 在每一条新的线路上分开 lines=text.splitlines 在p标记中换行 对于行中的i: 结果。附加“”+i+”

” 返回结果
这将返回一个新列表,而我需要两个html段落作为字符串,甚至一个没有新行。看起来第二个答案是使用.join实现的。是的,那么@DroidX86的解决方案更适合您。这将返回一个新列表,而我需要两个html段落作为字符串,甚至一个没有新行的段落。看起来这里的第二个答案是使用.join实现的。是的,那么@DroidX86的解决方案更适合您。就是这样!谢谢@DrioidX86@rdas非常感谢。我在我的帖子上并没有得到任何爱,但持续不断的搜索终于把我带到了这里,找到了一个答案,我可以随便摆弄一下,我就可以真正开始工作了!如果你想因为回答这个问题而获得荣誉,过来用你的智慧给我们增光添彩吧。我还没脱离险境,但现在我有了一条面包屑的踪迹。就是这样!谢谢@DrioidX86@rdas非常感谢。我在我的帖子上并没有得到任何爱,但持续不断的搜索终于把我带到了这里,找到了一个答案,我可以随便摆弄一下,我就可以真正开始工作了!如果你想因为回答这个问题而获得荣誉,过来用你的智慧给我们增光添彩吧。我还没有脱离险境,但现在我有了一条面包屑的踪迹。