I'm using sqlite3 in a Python script to extract data from a client's spreadsheet. My client is planning to add on to the spreadsheet, so my sqlite code should generate its columns based on the headers I extract from the first line. How do I do this? This is my naive attempt:
import sqlite3
conn = sqlite3.connect('./foo.sql')
c = conn.cursor()
for line in file:
if line[0] == 'firstline':
# Below is the line in question
c.execute(""" create table if not exists bar(?, ? ,?); """, lineTuple)
else:
c.execute(""" insert into bar values (?, ?, ?); """, lineTuple)
asked Sep 20, 2011 at 2:34
Owen Pierce
7832 gold badges11 silver badges25 bronze badges
1 Answer 1
I think, csv module of python can help you to extract file data.
First, convert your spreadsheet in csv format (save as csv command) with appropriate delimiter.
then, try below code snippet:
import csv
file_ptr = open('filename.csv','r');
fields = range(0, total number of columns in file header)
file_data = csv.DictReader(file_ptr, fields, delimiter=',')
for data in file_data:
print data
# data will be in dict format and first line would be all your headers,else are column data
# here, database query and code processing
Hope, it will be helpful.
answered Sep 20, 2011 at 7:19
Yajushi
1,1952 gold badges10 silver badges24 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default
create tablecreeps me out because then, in this case, you spend your time debugging the trash (spaces, special characters, unicode, newlines, etc.) the users put into the column headings.