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

时间:2011-08-05 12:30:14

标签: python regex

我有一个输入文件,其中包含一个包含许多五位数ID的javascript代码。我希望将这些ID放在如下列表中:

  

53231,53891,72829等

这是我的实际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,就像我想要的那样?

由于

1 个答案:

答案 0 :(得分:11)

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)