gpt4 book ai didi

python - 将文本区域输入转换为分段 HTML

转载 作者:太空宇宙 更新时间:2023-11-04 10:52:30 24 4
gpt4 key购买 nike

我想将用户在 html 页面上的文本区域中输入的内容转换为 <p> -标记输出,其中每个 <p>正在更换新线路。

我正在尝试使用正则表达式,但我无法让它工作。有人会纠正我的表情吗?

String = "Hey, this is paragraph 1 \n and this is paragraph 2 \n and this will be paragraph 3"
Regex = r'(.+?)$'

它只会导致 Hey, this is paragraph 1 \n and this is paragraph 2 \n<p>and this will be paragraph 3</p>

最佳答案

我不会为此使用正则表达式,只是因为您不需要它。检查一下:

text = "Hey, this is paragraph 1 \n and this is paragraph 2 \n and this will be paragraph 3"
html = ''
for line in text.split('\n'):
html += '<p>' + line + '</p>'

print html

让它成为一行,因为越短越好,也越清晰:

html = ''.join('<p>'+L+'</p>' for L in text.split('\n'))

关于python - 将文本区域输入转换为分段 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12763711/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com