Python 的 Dominate 库中是否有类似于 .replace() 的函数?
Is there a function similar to .replace() in Dominate library for Python?
我想将 HTML 标签添加到从 .txt 文件中提取的文本,然后另存为 HTML。我正在尝试查找特定单词的任何实例,然后 'replace' 它与锚标记内的相同单词。
像这样:
import dominate
from dominate.tags import *
item = 'item1'
text = ['here is item1 in a line of text', 'here is item2 in a line too']
doc = dominate.document()
with doc:
for i, line in enumerate(text):
if item in text[i]:
text[i].replace(item, a(item, href='/item1'))
以上代码报错:
TypeError: replace() argument 2 must be str, not a.
我可以做到这一点:
print(doc.body)
<body>
<p>here is item1 in a line of text</p>
<p>here is item2 in a line too</p>
</body>
但我想要这个:
print(doc.body)
<body>
<p>here is <a href='/item1'>item1</a> in a line of text</p>
<p>here is item2 in a line too</p>
</body>
Dominate 中没有 replace() 方法,但此解决方案适用于我想要实现的目标:
- 将锚标记创建为字符串。此处存储在变量 'item_atag':
item = 'item1'
url = '/item1'
item_atag = '<a href={}>{}</a>'.format(url, item)
- 使用 Dominate 库将原文中的每一行包裹段落标签,然后转换为字符串:
text = ['here is item1 in a line of text', 'here is item2 in a line too']
from dominate import document
from dominate.tags import p
doc = document()
with doc.body:
for i, line in enumerate(text):
p(text[i])
html_string = str(doc.body)
- 使用Python的内置字符串replace()方法添加锚标签:
html_with_atag = html_string.replace(item, item_atag)
- 最后,将新字符串写入 HTML 文件:
with open('html_file.html', 'w') as f:
f.write(html_with_atag)
我想将 HTML 标签添加到从 .txt 文件中提取的文本,然后另存为 HTML。我正在尝试查找特定单词的任何实例,然后 'replace' 它与锚标记内的相同单词。
像这样:
import dominate
from dominate.tags import *
item = 'item1'
text = ['here is item1 in a line of text', 'here is item2 in a line too']
doc = dominate.document()
with doc:
for i, line in enumerate(text):
if item in text[i]:
text[i].replace(item, a(item, href='/item1'))
以上代码报错:
TypeError: replace() argument 2 must be str, not a.
我可以做到这一点:
print(doc.body)
<body>
<p>here is item1 in a line of text</p>
<p>here is item2 in a line too</p>
</body>
但我想要这个:
print(doc.body)
<body>
<p>here is <a href='/item1'>item1</a> in a line of text</p>
<p>here is item2 in a line too</p>
</body>
Dominate 中没有 replace() 方法,但此解决方案适用于我想要实现的目标:
- 将锚标记创建为字符串。此处存储在变量 'item_atag':
item = 'item1'
url = '/item1'
item_atag = '<a href={}>{}</a>'.format(url, item)
- 使用 Dominate 库将原文中的每一行包裹段落标签,然后转换为字符串:
text = ['here is item1 in a line of text', 'here is item2 in a line too']
from dominate import document
from dominate.tags import p
doc = document()
with doc.body:
for i, line in enumerate(text):
p(text[i])
html_string = str(doc.body)
- 使用Python的内置字符串replace()方法添加锚标签:
html_with_atag = html_string.replace(item, item_atag)
- 最后,将新字符串写入 HTML 文件:
with open('html_file.html', 'w') as f:
f.write(html_with_atag)