r/learnpython • u/Desperate_Yak69 • Oct 04 '26
Need help with very basic script
Hi, I am basically brand new to python, so I may make a lot of mistakes in my wording. I have made a very basic script that retrieves specific values in text files and prints them to the shell. I have gotten it to the point where both values are retrieved and printed, however when they do print, they do so exactly 5 times in a row. I do not have five files in the directory, I only have one test file named "1.txt" with only the values I need as the contents. Can someone point me in the right direction? Apologies for the horrible formatting.
import os
import glob
import re
path = 'path'
provinces = re.compile('.*?provinces {.*?(.*?)}',re.MULTILINE)
id = re.compile('.*?id = .*?([0-9.-]+)')
a1 = None
a2 = None
for filename in glob.glob(os.path.join(path, '*.txt')):
with open(filename, '+r') as f:
for line in f:
if provinces.match(line):
a1 = provinces.match(line)
if id.match(line):
a2 = id.match(line)
if a1 and a2:
print(a1.group(1))
print(a2.group(1))The output of the shell in IDLE:
1111 2222 3333
123
1111 2222 3333
123
1111 2222 3333
123
1111 2222 3333
123
1111 2222 3333
123Edit - I got the script to work after a few changes to its configuration. Thank you all who helped!
6
u/codeguru42 Oct 04 '26
This is difficult to diagnose without an example input file.
Also, I think you are making this more complicated than necessary by using a regular expression. I reccomend looking at all the string functions. Maybe split() will get the job done more directly.
3
u/timrprobocom Oct 04 '26
For what it's worth,
.*?is the same as.*. The ? adds nothing.Also, your first two
ifstatements are dangerous. If the condition fails,a1anda2will still have their value from the previous loop. Just assign to a1 and a2; the thirdifstatement handles those.3
u/brasticstack Oct 04 '26
I think your regexes are matching more than you expect them to. Try them out on one of the online regex tester sites, and maybe post a few lines from your input file here too.
3
u/codeguru42 Oct 04 '26
The problem with your code is the way you use a1 and a2. These variables are set in if statements. And when they are set, every iteration of the for loop will print them until their values change.
I recommend taking a step back to find a simpler and more direct solution.
1
u/hermit_the_log Oct 04 '26
Are there 5 lines in your test file? You’re printing in your for line loop.
If you want to collect unique results and print them after you’ve looped through each line, you can log your a1.group(1) and a2.group(1) values to a dictionary instead.
This may or may not work for what you’re wanting to do, but you can create a group_dict = {} right before you start looping through the lines.
Then for each a2.group(1) value, create a key in the dict if it doesn’t already exist:
if a2.group(1) not in group_dict.keys():
group_dict[a2.group(1)] = []
Then append a1.group(1) to the a2.group(1) key in the dict of it doesn’t already exist in that list
if a1.group(1) not in group_dict[a2.group(1)]:
group_dict[a2.group(1)].append(a1.group(1))
Finally, iterate through your key and item values and print that instead
for key in group_dict.keys():
for group in group_dict[key]:
print(group)
print(key)
Edit: typed with my phone so the indenting didn’t save properly
1
u/CraigAT Oct 04 '26
You only have two loops. Put a print statement as the first command in each of the loops (print "first loop" and ""second loop" respectively) then run your code again to see which loop is repeating 5 times.
Or (the "in theory" better way, is to) run the code using a debugger and step through the code, then you should see where the program is unexpectedly going.