【问题标题】:Why my code python only can import some data from a file .CSV to my database PostgreSQL?为什么我的代码 python 只能将文件 .CSV 中的一些数据导入我的数据库 PostgreSQL?
【发布时间】: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


    【解决方案1】:
    reader.__next__
    

    这只是检索方法,它不调用方法。你需要reader.__next__(),但我认为next(reader) 可能更传统。

    0765335344,Vicious,"V.E. Schwab, Victoria Schwab",2013
    

    对我来说很好用。也许你的实际文件有智能引号或类似的东西,而不是直接的 ASCII。

    【讨论】:

    • 感谢您的输入。next(reader) 语句允许我跳过包含列名称(isbn、title、author、year)的第一行,但是当它找到数据时传输仍然停止包含双引号和逗号,我需要将它们导入我的 PostgreSQL 数据库。
    • 这是新结果:C:\xampp\htdocs\project1>python import0_pr1A.py 添加了《雅典娜的印记》一书 添加了《Her Fearful Symmetry Traceback》一书(最近一次调用最后一次):文件“ import0_pr1A.py”,第 24 行,在 main() 文件“import0_pr1A.py”,第 16 行,主要用于 isbn、标题、作者、读者年份:ValueError:没有足够的值来解包(预期为 4,得到1) C:\xampp\htdocs\project1>
    • 文件 .CSV 有几个双引号和逗号的数据,它不允许所有表都可以成功导入。
    • 我一直在为 reader() 函数排练一些参数,以自定义其工作方式(skipinialspace、quotechar、quoting、doublequote),但结果是一样的。突然间我不知道如何参数化这些参数了。
    【解决方案2】:

    试试

    csv.reader(lines, quotechar='"', delimiter=',', ...
    

    csv.readerprior SO回答,

    【讨论】:

    • 感谢您的意见。我用几种方法对此进行了探索,但它仍然不起作用: reader = csv.reader(f, quotechar='"', delimiter=',', quoting=csv.QUOTE_ALL, skipinitialspace=True)
    • 那么对不起。我的 Python 很弱,但我认为它有机会。
    • 它开始导入数据,但由于数据带有双引号和逗号,它仍然突然停止。 C:\xampp\htdocs\project1>python import0_pr1A.py 添加了 Nicholas 和 Alexandra 的书文件“import0_pr1A.py”,第 17 行,主要用于 isbn、标题、作者、读者年份:ValueError: not enough values to unpack (expected 4, got 1)
    • 例如这是因为第二个数据不允许传输而停止传输的行:140690483X,"Right Ho, Jeeves",P.G.伍德豪斯,1934 年
    • 我认为这些参数(引号、分隔符、引用、skipinitialspace、分隔符等)可以起作用,但需要另一种配置。
    猜你喜欢
    • 2017-03-08
    • 2020-05-12
    • 2018-11-07
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 2021-10-20
    相关资源
    最近更新 更多