Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Novice use of a CSV
#1
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.
Reply
#2
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,311 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  Python Complete novice: Want to run a vulnerability script bhanney23 1 2,978 Sep-18-2020, 09:27 AM
Last Post: Aspire2Inspire
  (Complete Novice) Code not working PythonGainz 9 4,097 Mar-21-2020, 05:53 PM
Last Post: buran
  Trivial novice question: information gathering tools in Python Drone4four 1 2,252 Nov-10-2018, 01:41 AM
Last Post: Larz60+
  I'm blocked in the construction of my program [Novice] abcd 1 2,626 May-22-2018, 06:02 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020