简体   繁体   English

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

[英]Python regex findall into output file

i got an inputfile which contains a javascript code which contains many five-figure ids. 我有一个输入文件,其中包含一个包含许多五位数ID的javascript代码。 I want to have these ids in a list like: 我希望将这些ID放在以下列表中:

53231,53891,72829 etc 53231,53891,72829等

This is my actual python file: 这是我的实际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")

How can i get these ids in the output file like i want it? 我怎么能像我想要的那样在输出文件中获取这些ID?

Thanks 谢谢

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM