for loop in for loop, python, csv reader/writer
Question: I am new in python, but after 3 days of reading and finding solution without
success i am lost.

I've got mysql table with data (id, user_id...). I connect to db, read user_id
and save data into array "user". Then i open csv file with a lot of rows and
columns (user_id, name, mail, telephone, address...). In the next step i
compare if user_id from db matches with user_id in csv file. If answer is yes
then i write this row in another csv file.

Problem is that my code works only for first id_user. Please, can you help me?

**Example table:**

    
    
    table in db:
    id     user_id
    1      318604
    2      318624
    

**csv file:**

    
    
    318604;    John;    john@gmail.com
    318604;    053746;  USA 
    318624;    Lucy;    Lucy@gmail.com
    318624;    058839   Sweeden
    318630;    Luke;    Luke@gmail.com
    

**Expected result in new file:**

    
    
    318604;    John;    john@gmail.com
    318604;    053746;  USA 
    318624;    Lucy;    Lucy@gmail.com
    318624;    058839   Sweeden
    

**Code:**

    
    
    cur = con.cursor()
    with open('input.csv', mode='rb') as f:
        reader = csv.reader(f, delimiter=';')
        with open('output.csv', mode='a') as w:  
            writer = csv.writer(w)
            with con:            
                cur.execute("SELECT user_id FROM users")
                user=cur.fetchall()            
                for i in range(len(user)):                               
                    for row in reader:
                        if(user[i][0]==row[0]):
                            writer.writerow(row)
    con.close()
    


Answer: You need to reopen the **input.csv** file. When you go through each line of
the file without reopening it your courser will always be on the last line of
the file. To fix that move opening of the input file to the `for user` loop:

    
    
    cur = con.cursor()
    with open('output.csv', mode='a') as w:  
        writer = csv.writer(w)
        with con:            
            cur.execute("SELECT user_id FROM users")
            user=cur.fetchall()            
            for i in range(len(user)): 
                with open('input.csv', mode='rb') as f:
                    reader = csv.reader(f, delimiter=';')                              
                    for row in reader:
                        if(user[i][0]==row[0]):
                            writer.writerow(row)
    con.close()
    

I tested it with this code:

    
    
    import csv
    user = [[1,318604],[2,318624]]
    with open('output.csv', mode='a') as w:  
        writer = csv.writer(w)      
        for i in range(len(user)):
            with open('input.csv', mode='rb') as f:
                reader = csv.reader(f, delimiter=';')                               
                for row in reader:
                    if(str(user[i][1])==row[0]):
                        writer.writerow(row)
    

Instead output from your database I used list with integers in it. So I needed
to convert `user[i][1]` to string. Also `user[i][0]` is `id`, when
`user[i][1]` is `user_id`, don't know if it the same for your output from
database.

Output:

    
    
    318604,    John,    john@gmail.com
    318604,    053746,  USA 
    318624,    Lucy,    Lucy@gmail.com
    318624,    058839   Sweeden
    

