Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regex findall into output file

Tags:

python

regex

i got an inputfile which contains a javascript code which contains many five-figure ids. I want to have these ids in a list like:

53231,53891,72829 etc

This is my actual python file:

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?

Thanks

like image 212
Florian Avatar asked Aug 05 '11 12:08

Florian


1 Answers

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)
like image 119
Tim Pietzcker Avatar answered Oct 08 '22 18:10

Tim Pietzcker