[英]Write to CSV from sqlite3 database in python
Ok, So I have a database called cars.db which has a table == inventory,好的,所以我有一个名为 cars.db 的数据库,它有一个表 == 库存,
Inventory essentially contains库存基本上包含
('Ford', 'Hiluz', 2),
('Ford', 'Tek', 6),
('Ford', 'Outlander', 9),
('Honda', 'Dualis', 3),
('Honday', 'Elantre', 4)
I then wrote this which is meant to edit that to the csv, however, I can't seem to work this out, in some cases I get stuff to print but its not right, and when I try and fix that, nothing prints.然后我写了这个,旨在将它编辑到 csv,但是,我似乎无法解决这个问题,在某些情况下,我可以打印一些东西,但它不正确,当我尝试修复它时,没有打印。 Any suggestions to get me on track?
有什么建议可以让我走上正轨吗?
#write table to csv
import sqlite3
import csv
with sqlite3.connect("cars.db") as connection:
csvWriter = csv.writer(open("output.csv", "w"))
c = connection.cursor()
rows = c.fetchall()
for x in rows:
csvWriter.writerows(x)
You should just do:你应该这样做:
rows = c.fetchall()
csvWriter.writerows(rows)
If the reason you iterate through the rows is because you wan't to preprocess them before writing them to the file, then use the writerow
method:如果您遍历行的原因是因为您不想在将它们写入文件之前对其进行预处理,则使用
writerow
方法:
rows = c.fetchall()
for row in rows:
# do your stuff
csvWriter.writerow(row)
import sqlite3
import csv
import os.path
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
db_path = os.path.join(BASE_DIR, "cars.db")
conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute("SELECT rowid, * FROM inventory")
columns = [column[0] for column in c.description]
results = []
for row in c.fetchall():
results.append(dict(zip(columns, row)))
with open("output.csv", "w", newline='') as new_file:
fieldnames = columns
writer = csv.DictWriter(new_file,fieldnames=fieldnames)
writer.writeheader()
for line in results:
writer.writerow(line)
conn.close()
Using Pandas should be more performant and requires less code.使用 Pandas 应该具有更高的性能并且需要更少的代码。 You can save the data from a sqlite table to a Pandas DataFrame and then use Pandas to write the CSV file.
您可以将 sqlite 表中的数据保存到 Pandas DataFrame,然后使用 Pandas 写入 CSV 文件。
df = pd.read_sql('SELECT * from cars', conn)
df.to_csv('cars.csv')
Here's the full code that creates your sqlite table with fake data:这是使用假数据创建 sqlite 表的完整代码:
import pandas as pd
import sqlite3
# create Pandas DataFrame
data = [('Toyota', 'Hilux', 2),
('Ford', 'Tek', 6),
('Ford', 'Outlander', 9),
('Honda', 'Dualis', 3),
('Honday', 'Elantre', 4)]
df = pd.DataFrame.from_records(data, columns=['make', 'model', 'age'])
# establish sqlite connection
conn = sqlite3.connect('../tmp/cars.db')
c = conn.cursor()
# create sqlite table
c.execute('''CREATE TABLE cars (make text, model text, age int)''')
# add data to sqlite table
df.to_sql('cars', conn, if_exists='append', index = False)
# write sqlite table out as a CSV file
df = pd.read_sql('SELECT * from cars', conn)
df.to_csv('../tmp/cars.csv')
Here's code to write out all the tables in a sqlite database as CSV files with a single command:这是使用单个命令将 sqlite 数据库中的所有表作为 CSV 文件写出的代码:
for table in c.execute("SELECT name FROM sqlite_master WHERE type='table';").fetchall():
t = table[0]
df = pd.read_sql('SELECT * from ' + t, conn)
df.to_parquet('../tmp/' + t + '.csv')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.