1

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;    [email protected]
318604;    053746;  USA 
318624;    Lucy;    [email protected]
318624;    058839   Sweeden
318630;    Luke;    [email protected]

Expected result in new file:

318604;    John;    [email protected]
318604;    053746;  USA 
318624;    Lucy;    [email protected]
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()

2 Answers 2

3

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,    [email protected]
318604,    053746,  USA 
318624,    Lucy,    [email protected]
318624,    058839   Sweeden
Sign up to request clarification or add additional context in comments.

Comments

-1

I think you have to take care of the format of the variable because you compare integer and string (from csv) , try this:

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")
            users=cur.fetchall()            
            for u in user:                               
                for row in reader:
                    if(str(u[0])==str(row[0])): 
                        writer.writerow(row)

seem's to work for me

2 Comments

Then why it print first time in the output file for only first user_id?
i'm not testing with database in fact , i test with list also. Try to iterate on recordset, i edit my answer

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.