【发布时间】:2020-07-13 05:27:57
【问题描述】:
描述和目标
这是 CS50 Web 编程课程的项目 1。 我需要通过 Python 将表的内容从文件 .csv 导入到我的数据库 PostgreSQL 中的表中。 该表具有以下格式:
isbn,title,author,year
0380795272,Krondor: The Betrayal,Raymond E. Feist,1998
表的列是直接在我的 PostgreQSL 数据库中使用下一个数据类型创建的:
id: Integer not null
isbn: Varchar not null
title: Text not null
author: Varchar not null
year: Integer not null
我有下一个 Python 代码:
import csv
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
def main():
f = open("bookspr1.csv")
reader = csv.reader(f)
for isbn, title, author, year in reader:
db.execute("INSERT INTO books (isbn, title, author, year) VALUES (:isbn, :title, :author, :year)",
{"isbn": isbn, "title": title, "author": author, "year": year})
print(f"Added the book {title}")
db.commit()
if __name__ == "__main__":
main()
问题
当我运行python代码从.csv文件中导入数据表时,系统报错:
C:\xampp\htdocs\project1>python import0_pr1A.py
Traceback (most recent call last):
File "C:\Users\Verel\AppData\Local\Programs\Python\Python37-32\lib\site-packag
es\sqlalchemy\engine\base.py", line 1284, in _execute_context
cursor, statement, parameters, context
File "C:\Users\Verel\AppData\Local\Programs\Python\Python37-32\lib\site-packag
es\sqlalchemy\engine\default.py", line 590, in do_execute
cursor.execute(statement, parameters)
psycopg2.errors.InvalidTextRepresentation: invalid input syntax for type integer
: "year"
LINE 1: ...le, author, year) VALUES ('isbn', 'title', 'author', 'year')
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "import0_pr1A.py", line 24, in <module>
main()
File "import0_pr1A.py", line 18, in main
{"isbn": isbn, "title": title, "author": author, "year": year})
File "C:\Users\Verel\AppData\Local\Programs\Python\Python37-32\lib\site-packag
es\sqlalchemy\orm\scoping.py", line 163, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "C:\Users\Verel\AppData\Local\Programs\Python\Python37-32\lib\site-packag
es\sqlalchemy\orm\session.py", line 1292, in execute
clause, params or {}
File "C:\Users\Verel\AppData\Local\Programs\Python\Python37-32\lib\site-packag
es\sqlalchemy\engine\base.py", line 1020, in execute
return meth(self, multiparams, params)
File "C:\Users\Verel\AppData\Local\Programs\Python\Python37-32\lib\site-packag
es\sqlalchemy\sql\elements.py", line 298, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "C:\Users\Verel\AppData\Local\Programs\Python\Python37-32\lib\site-packag
es\sqlalchemy\engine\base.py", line 1139, in _execute_clauseelement
distilled_params,
File "C:\Users\Verel\AppData\Local\Programs\Python\Python37-32\lib\site-packag
es\sqlalchemy\engine\base.py", line 1324, in _execute_context
e, statement, parameters, cursor, context
File "C:\Users\Verel\AppData\Local\Programs\Python\Python37-32\lib\site-packag
es\sqlalchemy\engine\base.py", line 1518, in _handle_dbapi_exception
sqlalchemy_exception, with_traceback=exc_info[2], from_=e
File "C:\Users\Verel\AppData\Local\Programs\Python\Python37-32\lib\site-packag
es\sqlalchemy\util\compat.py", line 178, in raise_
raise exception
File "C:\Users\Verel\AppData\Local\Programs\Python\Python37-32\lib\site-packag
es\sqlalchemy\engine\base.py", line 1284, in _execute_context
cursor, statement, parameters, context
File "C:\Users\Verel\AppData\Local\Programs\Python\Python37-32\lib\site-packag
es\sqlalchemy\engine\default.py", line 590, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.DataError: (psycopg2.errors.InvalidTextRepresentation) invalid in
put syntax for type integer: "year"
LINE 1: ...le, author, year) VALUES ('isbn', 'title', 'author', 'year')
^
[SQL: INSERT INTO books (isbn, title, author, year) VALUES (%(isbn)s, %(title)s,
%(author)s, %(year)s)]
[parameters: {'isbn': 'isbn', 'title': 'title', 'author': 'author', 'year': 'yea
r'}]
(Background on this error at: http://sqlalche.me/e/9h9h)
C:\xampp\htdocs\project1>
为了隔离问题,我尝试导入文件 .CSV 但将包含列名称(isbn、标题、作者、年份)的第一行放在首位,当我运行代码时,它会启动数据转移但是当它尝试导入数据“标题”或“作者”包含双引号(“”)和逗号(,)的行时突然停止并出现另一个错误.例如,作者 “V.E. Schwab, Victoria Schwab” 的下一行会产生这种冲突:
0765335344,Vicious,"V.E. Schwab, Victoria Schwab",2013
而新的错误是这样的:
C:\xampp\htdocs\project1>python import0_pr1A.py
Added the book The Mark of Athena
Added the book Her Fearful Symmetry
Traceback (most recent call last):
File "import0_pr1A.py", line 24, in <module>
main()
File "import0_pr1A.py", line 16, in main
for isbn, title, author, year in reader:
ValueError: not enough values to unpack (expected 4, got 1)
C:\xampp\htdocs\project1>python import0_pr1A.py
当导入的文件 .CSV 没有第一行(isbn、标题、作者、年份)并且没有包含 双引号 (" ") 和 的数据时,数据传输成功完成>逗号 (,).
C:\xampp\htdocs\project1>python import0_pr1A.py
Added the book The Lion's Game
Added the book The Rainmaker
Added the book Eleanor & Park
C:\xampp\htdocs\project1>python import0_pr1A.py
C:\xampp\htdocs\project1>python list0_pr1.py
Krondor: The Betrayal by Raymond E. Feist of 1998.
The Dark Is Rising by Susan Cooper of 1973.
The Black Unicorn by Terry Brooks of 1987.
The Lion's Game by Nelson DeMille of 2000.
The Rainmaker by John Grisham of 1995.
Eleanor & Park by Rainbow Rowell of 2013.
C:\xampp\htdocs\project1>
最后我尝试插入一些代码行,但结果是一样的:
导入 psycopg2
读者。下一个
db.close()
import csv
import os
import psycopg2
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
def main():
f = open("books.csv")
reader = csv.reader(f)
reader.__next__
for isbn, title, author, year in reader:
db.execute("INSERT INTO books (isbn, title, author, year) VALUES (:isbn, :title, :author, :year)",
{"isbn": isbn, "title": title, "author": author, "year": year})
print(f"Added the book {title}")
db.commit()
db.close()
if __name__ == "__main__":
main()
结论 我需要帮助来修改这个 python 代码,让我完全导入文件 .csv,包括第一行和包含 双引号 (" ") 和 逗号 (,) 的数据强>。
【问题讨论】:
标签: python-3.x postgresql csv sqlalchemy