0

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.

3
  • input2 is 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. Commented Feb 12, 2018 at 21:43
  • files-objects are single-pass iterators, which is why in your second-iteration of your for-loop it's already exhausted (technically, you can reset a file-iterator using file_obj.seek(0)) Commented Feb 12, 2018 at 21:44
  • @Midi_: fix the indentation please Commented Feb 12, 2018 at 21:49

1 Answer 1

1

Iterators can only be used once. Move:

with open('file2.csv') as resultscsvfile:
    input2 = csv.reader(resultscsvfile, delimiter=',')

inside the for row in input1: loop to recreate it every single time you want to go through all the input2 rows.

btw, num2 != None is discouraged. Use num2 is not None instead.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! when i did that i get the error NameError: name 'resultscsvfile' is not defined
Its working perfectly, thank you. Thanks also for the != advice too i made those changes as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.