Python Forum
Novice use of a CSV - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Novice use of a CSV (/thread-23382.html)



Novice use of a CSV - preliator - Dec-26-2019

Good morning all,

Having a good knowledge of databases and SQL, I would like to be able to automate the task of recovering data from a CSV file with python.

The idea would be to have a csv file containing a variable number of columns and lines of the type:

NAME, FIRST NAME, BIRTH; CITY; AGE
CASTEL; JULIETTE; 10/12/2000; LYON; 15
COHEN; PABLO; 16/05/1980; LYON; 78
AL BELA; ABDEL; 02/09/2001; LA CIOTAT; 23
CHAPELON; FRED; 03/10/2012; PARIS; 56


... and create an output .txt file containing this script:

insert into NOM_DE_LA_TABLE (name, first name, birth, city, age) values
('CASTEL', 'JULIETTE', '10 / 12/2000 ',' LYON ', 15),
('COHEN', 'PABLO', '16 / 05/1980 ',' LYON ', 78),
etc ...
Unfortunately, I'm not sure how to do it.

Thank you and happy holidays.


RE: Novice use of a CSV - Axel_Erfurt - Dec-26-2019

you can use pandas and sqlite3

import pandas, sqlite3
csvfile = '/path/file.csv'
conn = sqlite3.connect('/path/file.sqlite')
df = pandas.read_csv(csvfile, sep=';') 
table_name = "yourTableName"
df.to_sql(table_name, conn, if_exists='append', index=False)