I’m trying to setup a local database on my laptop using python. I saw the tutorial on the internet and need to know few things before I get onto this:-
import sqlite3
conn = sqlite3.connect('example.db')
c = conn.cursor()
# Create table
c.execute('''CREATE TABLE stocks
(date text, trans text, symbol text, qty real, price real)''')
# Insert a row of data
c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")
conn.commit()
c.close()
In the above code, The data is inserted manually. But I have .csv file in my local drive and the data in that file is separated by space.
how do i insert the data in that file into the database? Also, I would like to extract data after that in excel format. Is it possible?
Thanks,
Mahi
Use the
csvmodule to read data from a CSV file. It has “dialect” settings that allow you to specify the column separator and quoting style. I recommend using thecsv.DictReaderclass to read in your data, since it allows you to refer to the read-in data by name.