csv进入sqlite表python

时间:2017-01-27 18:23:06

标签: python sqlite csv

使用python,我试图将csv导入sqlite表并使用csv文件中的头文件成为sqlite表中的头文件。代码运行但似乎没有创建表“MyTable”。这是代码:

import urllib
url = "https://www.mywebsite.com/"
# Using with statements properly for safe resource cleanup
with urllib.urlopen(url) as onlinepage:
    onlinedata = onlinepage.read()
print(onlinedata)

with open("offline.txt", "r+") as offlinepage:  # DOES NOT TRUNCATE EXISTING FILE!
    offlinedata = offlinepage.read()
    print(offlinedata)

    if onlinedata == offlinedata:
        print("same") # for debugging
    else:
        print("different")
        # I assume you want to rewrite the local page, or you wouldn't open with +
        # so this is what you'd do to ensure you replace the existing data correctly
        offlinepage.seek(0)     # Ensure you're seeked to beginning of file for write
        offlinepage.write(onlinedata)
        offlinepage.truncate()  # If online data smaller, don't keep offline extra data

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:3)

您可以使用Pandas轻松实现(您可能需要先pip install pandas):

import sqlite3
import pandas as pd

# load data
df = pd.read_csv('dict_output.csv')

# strip whitespace from headers
df.columns = df.columns.str.strip()

con = sqlite3.connect("city_spec.db")

# drop data into database
df.to_sql("MyTable", con)

con.close()

Pandas将为您完成所有艰苦的工作,包括创建实际的表格!

答案 1 :(得分:0)

你也可以用peewee orm轻松完成。为此,您只使用peewee的扩展名,即playhouse.csv_loader:

from playhouse.csv_loader import *

db = SqliteDatabase('city_spec.db')

Test = load_csv(db, 'dict_output.csv')

您创建了数据库city_spec.db,其标题为字段,数据来自dict_output.csv

如果您没有小便,可以使用

进行安装
pip install peewee

答案 2 :(得分:0)

你还没有标明你的答案已经解决了,所以这里有。

只需连接一次数据库,只创建一次光标。 您只能读取一次csv记录。 我添加了代码,它仅根据列名创建数据库表的粗略形式。同样,这只在循环中完成一次。 您的插入代码工作正常。

import sqlite3
import csv

con = sqlite3.connect("city_spec.sqlite") ## these statements belong outside the loop
cursor = con.cursor()  ## execute them just once

first = True
with open ('dict_output.csv', 'r') as f:
    reader = csv.reader(f)
    columns = next(reader)
    columns = [h.strip() for h in columns]
    if first:
        sql = 'CREATE TABLE IF NOT EXISTS MyTable (%s)' % ', '.join(['%s text'%column for column in columns])
        print (sql)
        cursor.execute(sql)
        first = False
    #~ for row in reader:    ## we will read the rows later in the loop
        #~ print(row)

    query = 'insert into MyTable({0}) values ({1})'
    query = query.format(','.join(columns), ','.join('?' * len(columns)))
    print(query)
    cursor = con.cursor()
    for row in reader:
        cursor.execute(query, row)
    con.commit()
    con.close()