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()