I have the following code, which gives me some unwanted results:
f = open(filename, 'r')
year = re.search(r'Popularity in (\d\d\d\d)', f.read())
namerank = re.findall(r'<tr align="right"><td>(\d+)</td><td>(\w+)</td><td>(\w+)</td>', f.read())
if year:
print(year.group(1))
else:
print('No year found')
if namerank:
for rank in namerank:
print(rank)
else:
print('No names found')
Output:
Popularity in 1990
No names found
However, when I add another f = open(filename, 'r')
in line 3:
f = open(filename, 'r')
year = re.search(r'Popularity in (\d\d\d\d)', f.read())
f = open(filename, 'r')
namerank = re.findall(r'<tr align="right"><td>(\d+)</td><td>(\w+)</td><td>(\w+)</td>', f.read())
namerank
now has the correct data, which prints on my console. Can anybody tell me why I have to open the file twice in order to get the correct data? Is there a better way to write this code?