CSV file to batch import data to SQLite.

Source: Internet
Author: User
Tags sqlite

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.