将 csv 读入数据库 SQLite3 ODO Python

Read csv into database SQLite3 ODO Python

我正在尝试使用 ODO、SQLite3 和 Python.

将 csv 读入新数据库中的新 table

我正在遵循这些指南:

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。我可以在文件夹中看到它。

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

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 文档,这成功地在新数据库中创建了一个新的 table 并将 csv 文件读入该数据库:

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] 中使用的来源:

https://docs.python.org/2/library/sqlite3.html python odo sql AssertionError: datashape must be Record type, got 0 * {...}

[2] 中使用的来源:

http://sebastianraschka.com/Articles/2014_sqlite_in_python_tutorial.html#creating-a-new-sqlite-database what is difference between .sqlite and .db file?

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

我发现文档网站中的文档和github中的文档不一样。请使用 github 版本作为参考。

NotImplementedError: Unable to parse uri to data resource

section 中提到错误。

您可以使用

解决

pip install odo[sqlite]pip install odo[sqlalchemy]

那么如果你使用windows和odo 0.5.0:

可能会遇到另一个错误

AttributeError: 'DiGraph object has no attribute 'edge'

安装 networkx 1.11 而不是 networkx 2.0 可以解决此错误。 (reference)

pip uninstall networkx
pip install networkx==1.11

希望对您有所帮助