Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

Read csv into database SQLite3 ODO Python

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

I am following these guides:

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

I am trying the following:

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)

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

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

Can you help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

Sources used in [1]:

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

Sources used in [2]:

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...