3

There is a csv file in the format.

x1  0  1  3  6
x2  4  9  4  5
x3  8  6  7  1

I want insert it into a database with three fields "x1","x2","x3" into the following data.

x1|x2|x3
0|4|8
1|9|6
3|10|7
6|5|1

I think that i have to transform the data when i read the csv file.

import sqlite3
db="test.sqlite"
con = sqlite3.connect(db)
cur = con.cursor()
dat=open("data","r")
for id,line in enumerate(dat.readlines()):
    ?

con.executemany('insert into test values(?,?,?)', value)

how to get the right format of value here?

1 Answer 1

2

Transpose the data with zip(*rows), remove the first line and insert with an .executemany() call:

import csv
import sqlite3

with open('data', 'r', newline='') as infh:
    rows = list(csv.reader(infh))
    columns = zip(*rows)
    next(columns, None)  # skip the first column

    with sqlite3.connect('test.sqlite') as conn:
        cursor = conn.cursor()
        cursor.executemany('insert into test values (?, ?, ?)', columns)
Sign up to request clarification or add additional context in comments.

8 Comments

a little bug here,it should be with open('data', 'r') as infh: not rb.
No, it should be opened in binary mode; this is intentional.
Unless you are using Python 3; use open('data', 'r', newline='') in that case. The csv module handles newlines directly and if you open the file in text mode (Python 2) or with universal line ending handling (Python 3) you bypass that functionality.
i am in python3.3 ,with open('data', 'r') as infh: works fine for me,with open('data', 'rb') can't.
I revise it into ` rows=[row[1:] for row in rows] col=zip(*rows)` ,it's ok now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.