How to select only certain sections from a tab-delimited file to put into a Database using sqlite3 in Python -


i have tab-delimited file need select columns populate database using sqlite3 module in python. in file have vertical line symbol "|" , in other cases column empty (i tried showing empty fields using space many times doesn't show, keep in mind fields can empty instead of having number). example here rows of how file looks like:

78 | 43 | texret | 453 | 4321 | 32 | 433 |
20 | 291 | texttt 372 | 228 | 19 | 999
121 | 46 | textee | 3882 | 322 | 432 | 63 |

you can see rows (which need treated 1 row of file) take 2 lines.

i put in new database build using sqlite3 2nd field (e.g. first row number 43), text field (i.e. 5th field in understanding), , 13th field (e.g. same row no. 433). have in terms of code:

import sqlite3  con = sqlite3.connect('database_1.db') cur = con.cursor() cur.execute('create table number10(address_no int, area text, street_no int, primary key (address_no))') line in open('my_tab_text.txt', 'r'):     fields = line.strip().split("\t")     address_no = fields[2]       area = fields[4]     street_no = fields[12]     data = [row row in fields]  cur.executemany("insert number10 (address_no, area, street_no) values (?, ?, ?);", (data)) con.commit() 

so above code, when run error: "sqlite3.programmingerror: incorrect number of bindings supplied. current statement uses 3, , there 14 supplied. in other words, counts in "|" symbol in well. don't know how work around it. tried substituting line with: fields = line.strip("|").split("\t")

but still gave me programmingerror less number of fields supplied.

the expected output create database named "number10.db" , this:

address_no area street_no 78 textret 433 20 textttt 19 121 textee 63

so please notice there rows have empty fields @ times. guess, need put default value of 0 there. appreciated.

instead of data = [row row in fields] (a list containing fields), want create list (or tuple) containing rows want:

data = (address_no, area, street_no) 

then pass data in have done.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -