我使用googlesheetsapi和python从电子表格中输入的数据生成HTML标记。有时用户在单个单元格中输入长文本块,我希望在出现新行时使用python将其解析为语义段落。你知道吗
通过使用str.splitlines()
和forloop,我可以让它在概念上工作,但是循环的第一次迭代是打印出来的。你知道吗
#!/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
for i in lines:
return '<p>'+i+'</p>'
print(pgparse(text))
结果:
<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>
此行退出函数。也许你想要:
你只回第一行。你的第二行从来没有包装过。 试试这个:
使用生成器表达式包装行,然后使用
\n
将它们连接回来相关问题 更多 >
编程相关推荐