As far as I see it right now, the enumerate function is necessary for my script. But since it only reads the txt file line by line I am not able to capture my RegEx pattern all the time. I would like to be able to read two lines at a time while using enumerate. Is this possible?
import re
import itertools
a_list = []
f = open("/Users/me/Desktop/scrape.txt", "r", encoding="utf8")
txt = f.readlines()
for k, line in enumerate(txt):
if re.findall(r'\w+,\s*f\s*\.\s*\d\s*\d\s*-\s*\d\s*\d\s*-\s*\d\s*\d\s*\d\s*\d', line):
a_list.append((k, line))
I have tried (and failed) to use islice() like so:
import re
import itertools
a_list = []
f = open("/Users/me/Desktop/scrape.txt", "r", encoding="utf8")
txt = f.readlines()
**while True:
next_two_lines = list(itertools.islice(f, 2))
if not next_two_lines:
break**
for k, line in enumerate(txt):
if re.findall(r'\w+,\s*f\s*\.\s*\d\s*\d\s*-\s*\d\s*\d\s*-\s*\d\s*\d\s*\d\s*\d', line):
a_list.append((k, line))
You can do
for k, line in enumerate(zip(txt[::2], txt[1::2])):
if re.findall(r'\w+,\s*f\s*\.\s*\d\s*\d\s*-\s*\d\s*\d\s*-\s*\d\s*\d\s*\d\s*\d', "{} {}".format(line[0], line[1])):
a_list.append((k, line))
That will iterate over tuples that each one of them contains two lines.