我正在使用一段Python
脚本遍历文本文件中的行。
我想在文本文档中查找img
标签并将其作为文本返回。
当我运行正则表达式re.match(line)
时,它会返回一个_sre.SRE_MATCH
对象。
我如何让它返回一个字符串?
import sys
import string
import re
f = open("sample.txt", 'r' )
l = open('writetest.txt', 'w')
count = 1
for line in f:
line = line.rstrip()
imgtag = re.match(r'<img.*?>',line)
print("yo it's a {}".format(imgtag))
运行时会打印:
yo it's a None
yo it's a None
yo it's a None
yo it's a <_sre.SRE_Match object at 0x7fd4ea90e578>
yo it's a None
yo it's a <_sre.SRE_Match object at 0x7fd4ea90e578>
yo it's a None
yo it's a <_sre.SRE_Match object at 0x7fd4ea90e578>
yo it's a <_sre.SRE_Match object at 0x7fd4ea90e5e0>
yo it's a None
yo it's a None
imgtag.group()
不使用索引也有效。 - guesswho