CSV file to batch import data to SQLite.
Code:
f = web.input (Bs_switch = {}) # Bs_switch is the name of the From Form file field
Data =[i.split (",") for I in f["Bs_switch"].file.read (). Split () [1:]]
#这里的步骤:
#1, read the imported content: f["Bs_switch"].file.read ()
#2, because the imported content is STR, converted to a list. With Split ().
#3, because the first column of the CSV file is a field name and is not required, it is sliced with [1:0].
#4, each item in the data list is a separate str, and a CSV delimiter is required to convert the data to list. Use the split (",") method.
#5, note that before importing, you need to set the encoding of CSV to Utf-8.
For I in data:
I[0] = conf.get_field_by_name ("Bs_line", "id", i[0]) #将字段名称转化为数据库中的id值.
Db2.query ("INSERT into Bs_switch (l_id,name,type,property,remarks) VALUES ('%s ', '%s ', '%s ', '%s ', '%s ')"%tuple (i))
CSV file to batch import data to SQLite.