Thank you for taking the time to read this. I am writing a python script that will check if phone numbers in one csv file are in another csv file. Right now I am just trying to print the comparisons out
num1 == num2
This is a brute force method. So another example would be if there were two arrays arr1 = [1,2,3] and arr2 = [4,5,6] I would want it to print out like this:
1 4
1 5
1 6
2 4
2 5
2 6
...
The problem: When I run this code for some reason the comparisons to the child loop are only happening once, then it just prints out the rest of the parent loop without running the child loop again and I cant figure out why. So it looks basically like this:
1 4
1 5
1 6
2
3
And then it ends. Any idea why?
Code
def get_number(filter):
try:
filter = re.sub('-', '', filter)
filter = re.sub(' ', '', filter)
filter = re.sub('\)', '', filter)
filter = re.sub('\(', '', filter)
filter = filter.replace("+", "")
if filter[:1] == "1":
filter = filter[1:]
if len(filter) != 10:
filter = None
if filter != None:
return int(filter)
except ValueError:
print("Oops! That was no valid number. Try again...")
with open('file1.csv') as csvfile, open('file2.csv') as resultscsvfile:
input1 = csv.reader(csvfile, delimiter=',')
input2 = csv.reader(resultscsvfile, delimiter=',')
for row in input1:
filter = row[29]
num1 = get_number(filter)
print("Num 1: " + str(num1))
if num1 != None and len(str(num1)) == 10:
for row2 in input2:
filter2 = row2[6]
num2 = get_number(filter2)
if num2 != None and len(str(num2)) == 10:
print(str(num1) + " == " + str(num2))
PS the get_number function is removing all of the additional characters and spaces to have a strait up comparison between numbers since people format phone numbers differently often.
input2is an iterator and is exhausted after the first inner loop. Open the file anew or cache the entire contents in a list before looping, if it's not too much.file_obj.seek(0))