以下是我的信息:
输入:
\"button\" \"button\" href=\"#\" data-id=\"11111111111\" \"button\" \"button\" href=\"#\" data-id=\"222222222222\"
\"button\" \"button\" href=\"#\"
我想要的输出:
11111111111
222222222222
我的第一个代码运行良好:
text = 'data-id=\"11111111111 \" data-id=\"222222222222\" '
c = re.findall('data-id=\\"(.*?)\\"', text)
我的第二个代码不起作用。什么也没显示
with open("E:/test.txt","r") as f:
text = f.readline()
c = re.findall('data-id=\\"(.*?)\\"', text)
为什么我的辅助代码不起作用。请帮我修复它。非常感谢您。谢谢 :)
请检查此答案。(在str_txt.txt文件中添加了两行)。
我在第二个代码中所做的唯一更改是,我在regex中以'r'作为前缀。有关正则表达式中'r'前缀的更多信息,请在此处检查!
import re
with open("str_txt.txt","r") as f:
text = f.readlines()
for line in text:
c=[]
c = re.findall(r'data-id=\\"(.*?)\\"', line)
print c
输出:
C:\Users\dinesh_pundkar\Desktop>python demo.Py
['11111111111', '222222222222']
['1111113434111', '222222222222']
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句