将.csv导入SQL

2024-06-23 02:42:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用CSV来使用Python填充一个34列的SQL数据库,尽管我做不到

import csv sqlite3
con = sqlite3.connect(":memory:")
cur = con.cursor()

cur.execute("CREATE TABLE t (No, Source, Host, Link, Date, Time, time2, Category, AuthorId, AuthorName, AuthorUrl, Auth, Followers, Following, Age, Gender, Language, Country, Province, City, Location, Sentiment, Title, Snippet, Description, Tags, Contents, View, Comments, Rating, Favourites, Duration, Bio, UniqueId);")}

with open('database.csv', 'rb') as fin:
    dr = csv.reader(fin) 
    dicts = ({'No': line[0], 'Source': line[1], 'Host': line[2], 'Link': line[3], 'Date': line[4], 'Time': line[5], 'time2': line[6], 'Category': line[7], 'AuthorId': line[8], 'AuthorName': line[9], 'AuthorUrl': line[10], 'Auth': line[11], 'Followers': line[12], 'Following': line[13], 'Age': line[14], 'Gender': line[15], 'Language': line[16], 'Country': line[17], 'Province': line[18], 'City': line[19], 'Location': line[20], 'Sentiment': line[21], 'Title': line[22], 'Snippet': line[23], 'Description': line[24], 'Tags': line[25], 'Contents': line[26], 'View': line[27], 'Comments': line[28], 'Rating': line[29], 'Favourites': line[30], 'Duration': line[31], 'Following': line[32], 'UniqueId': line[33]} for line in dr)
    to_db = ((i['No'], i['Source'], i['Host'], i['Link'], i['Date'], i['Time'], i['time2'], i['Category'], i['AuthorId'], i['AuthorName'], i['AuthorUrl'], i['Auth'], i['Followers'], i['Following'], i['Age'], i['Gender'], i['Language'], i['Country'], i['Province'], i['City'], i['Location'], i['Sentiment'], i['Title'], i['Snippet'], i['Description'], i['Tags'], i['Contents'], i['View'], i['Comments'], i['Rating'], i['Favourites'], i['Duration'], i['Bio'], i['UniqueId']) for i in dicts)

cur.executemany("INSERT INTO t VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", to_db)
con.commit()

我已经观察了很多迹象,尽管这是我第一次尝试Python,但我不知道该怎么做。在

你能帮我拿这个吗?非常感谢。在

Pd:如果无法推断,csv文件没有标题,我正在尝试一次一列一列地填充。在


Tags: csvnohostsourcedatetimelinelink
2条回答

如果CSV元素的位置是正确的,你能不能做一些更直接的事情,例如用下面的数据作为例子

1,2,3
a,b,c

使用以下方法:

^{pr2}$

这很管用。我用了一些捷径来节省打字的时间。在

import csv
import sqlite3
import itertools

params = ['No', 'Source', 'Host', 'Link', 'Date', 'Time', 'time2', 'Category', 'AuthorId', 'AuthorName', 'AuthorUrl', 'Auth', 'Followers', 'Following', 'Age', 'Gender', 'Language', 'Country', 'Province', 'City', 'Location', 'Sentiment', 'Title', 'Snippet', 'Description', 'Tags', 'Contents', 'View', 'Comments', 'Rating', 'Favourites', 'Duration', 'Bio', 'UniqueId']

create_str = "CREATE TABLE t (%s);" % ', '.join('"%s"' % p for p in params)
insert_str = "INSERT INTO t VALUES (%s)" % ', '.join(itertools.repeat('?', len(params)))

with open('database.csv') as fin:
    dr = csv.DictReader(fin, fieldnames=params, skipinitialspace=True)
    lst = [tuple(d[p] for p in params) for d in dr]

con = sqlite3.connect(":memory:")
cur = con.cursor()
cur.execute(create_str)

cur.executemany(insert_str, lst)
con.commit()

for row in cur.execute("select * from t;"):
    print(row)

注意使用字符串格式操作来构建sql查询字符串的错误做法。如果与未知输入数据一起使用,则可能导致sql注入攻击。我之所以在这里这样做是因为字符串是根据已知值构建的,而未知输入(来自文件的)是使用标准“?”正确构建的传递给execute方法的元组占位符。在

请注意,一个表中的参数太多了。它应该在多个表中更加规范化,但我想您在某个时候会了解到这一点。在

相关问题 更多 >