2011-08-05 14 views
6

私は多くの5桁のIDを含むjavascriptコードを含むinputfileを取得しました。私はこれらのIDを取得できますかPythonの正規表現findallを出力ファイル

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") 

:これは私の実際のpythonファイルである

53231,53891,72829など

:私のようなリストにこれらのIDを持つようにしたいです私はそれを望むような出力ファイルですか?答えは、問題を解決するかどうか

おかげ

答えて

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) 
+0

おかげで多くは、(カウント票以下の 'V'マーク)の答えを受け入れることを検討@Florian – Florian

+0

を働きました。 – MatToufoutu