Python正则表达式找到输出文件
发布时间:2020-12-16 23:37:20 所属栏目:Python 来源:网络整理
导读:我有一个输入文件,其中包含一个包含许多五位数ID的 javascript代码.我希望将这些ID放在以下列表中: 53231,53891,72829 etc 这是我的实际python文件: import refobj = open("input.txt","r")text = fobj.read()output = re.findall(r'[0-9][0-9][0-9][0-9][
我有一个输入文件,其中包含一个包含许多五位数ID的
javascript代码.我希望将这些ID放在以下列表中:
这是我的实际python文件: import re fobj = open("input.txt","r") text = fobj.read() output = re.findall(r'[0-9][0-9][0-9][0-9][0-9]',text) outp = open("output.txt","w") 我怎么能像我想要的那样在输出文件中获取这些ID? 谢谢 解决方法import re # Use "with" so the file will automatically be closed with open("input.txt","r") as fobj: text = fobj.read() # Use word boundary anchors (b) so only five-digit numbers are matched. # Otherwise,123456 would also be matched (and the match result would be 12345)! output = re.findall(r'bd{5}b',text) # Join the matches together out_str = ",".join(output) # Write them to a file,again using "with" so the file will be closed. with open("output.txt","w") as outp: outp.write(out_str) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |