簡體   English   中英

Python正則表達式找到輸出文件

[英]Python regex findall into output file

我有一個輸入文件,其中包含一個包含許多五位數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?

謝謝

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