Answer the question
In order to leave comments, you need to log in
How to parse a file line by line, catching and overwriting my pattern?
import re
f1 = open("/home/tarlis/ParserTest/2.txt", 'r')
f2 = open("/home/tarlis/ParserTest/1.txt", "a")
fr = f1.read()
reg_pattern = 'title=\"(\D+)\"\D*data=\"([a-z.][email protected][mailstbknox]+\.ru)'
for line in fr:
match = re.search(reg_pattern, line)
if match is not None:
f2.write(match.group(1) + '|' + match.group(2) + '\n')
f2.close()
f1.close()
<div>
<a target="_blank" " title="Дмитрий" data="[email protected]">Дмитрий </a>
</div>
Answer the question
In order to leave comments, you need to log in
f1.read() - read the file at once in a line. The subsequent loop will traverse the string.
f1 is iterable, you can do something like this
for line in f1:
match.re.search(r, line)
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question