您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python正则表达式找到输出文件

5b51 2022/1/14 8:23:17 python 字数 1195 阅读 587 来源 www.jb51.cc/python

我有一个输入文件,其中包含一个包含许多五位数ID的 javascript代码.我希望将这些ID放在以下列表中: 53231,53891,72829 etc 这是我的实际python文件: import re fobj = open("input.txt", "r") text = fobj.read() output = re.findall(r'[0-9][0-9][0-9][0-9][0-

概述

53231,53891,72829 etc

这是我的实际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'\b\d{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)


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶