我想向该文件添加两个html标记。每行都以<br>
类似的方式结束
>>> f = open("/tmp/x","r")
>>> con = f.readlines()
>>> for line in con:
... print line + "<br>"
...
现在,我想替换"
为特定的html标签<h3>
。前,我有内容的文件
This is test file named "file.txt" and below is the way to
understand its data and values etc
This is line "one" is also "tricky" to change
预期输出为:
This is test file named <h3>file.txt</h3> and below is the way to <br>
understand its data and values etc<br>
This is line <h3>one</h3> is also <h3>tricky</h3> to change <br>
我正在考虑为“”显示两个标志。如果其奇数,请使用<h3>,否则请使用</ h3>。如果您有其他解决方案,请提出建议。
随着import re
你的代码的顶部,
line = re.sub(r'"([^"]*)"', r'<h3>\1</h3>', line)
应该做您在示例中显示的替换。
这不赶上一个“额外的/奇/备用”的发生"
,只有成对的的"
秒-如果你需要做一些其他替代的“奇”双引号如果有的话,那是很容易安排为处理的下一步骤为line
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句