将csv读入数据库SQLite3 ODO Python [英] Read csv into database SQLite3 ODO Python

查看:162
本文介绍了将csv读入数据库SQLite3 ODO Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ODO,SQLite3和Python将CSV读入新数据库中的新表中.

I am trying to read in a csv into a new table in a new databased using ODO, SQLite3 and Python.

我正在遵循以下指南:

https://media.readthedocs.org/pdf/odo/latest/odo.pdf http://odo.pydata. org/en/latest/perf.html?highlight = sqlite#csv-sqlite3-57m-31s

我正在尝试以下操作:

import sqlite3
import csv
from odo import odo

file_path = 'my_path/'
# In this case 'my_path/' is a substitute for my real path

db_name = 'data.sqlite'

conn = sqlite3.connect(file_path + db_name)

这将在file_path中创建一个新的sqlite文件data.sqlite.我可以在文件夹中看到它.

This creates a new sqlite file data.sqlite within file_path. I can see it there in the folder.

当我尝试将csv读入该数据库时,出现以下错误:

When I then try to read my csv into this database I get the following error:

csv_path = 'my_path/data.csv'
odo(csv_path, file_path + db_name)
conn.close()

NotImplementedError: Unable to parse uri to data resource: # lists my path

你能帮忙吗?

推荐答案

不用了ODO文档,它成功在新数据库中创建了一个新表,并将csv文件读入该数据库:

No thanks to the ODO documentation, this succesfully created a new table in a new database and read in the csv file to that database:

import sqlite3
import csv
from odo import odo

# [1]

#  Specify file path
file_path = 'my_path/'
# In this case 'my_path/' is a substitute for my real path

# Specify csv file path and name
csv_path = file_path + 'data.csv'

# Specify database name
db_name = 'data.sqlite'

# Connect to new database
conn = sqlite3.connect(file_path + db_name)

# [2]

# Use Odo to detect the shape and datatype of your csv:
data_shape = discover(resource(csv_path))

# Ready in csv to new table called 'data' within database 'data.sqlite'
odo(pd.read_csv(csv_path), 'sqlite:///' + file_path + 'data.sqlite::data', dshape=data_shape)

# Close database
conn.close()

[1]中使用的来源:

Sources used in [1]:

https://docs.python.org/2/library/sqlite3.html python odo sql AssertionError:datashape必须为记录类型,得到0 * {...}

[2]中使用的来源:

Sources used in [2]:

https://stackoverflow.com/a/41584832/2254228 http://sebastianraschka.com/Articles/2014_sqlite_in_python_tutorial.html#创建一个新的SQLite数据库 https://stackoverflow.com/a/33316230/2254228 .sqlite和.db文件之间有什么区别?

ODO文档在这里(祝您好运...) https://media.readthedocs.org/pdf/odo/latest/odo.pdf

The ODO documentation is here (good luck...) https://media.readthedocs.org/pdf/odo/latest/odo.pdf

这篇关于将csv读入数据库SQLite3 ODO Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆