简体   繁体   English

为什么我的代码 python 只能从文件中导入一些数据。CSV 到我的数据库 PostgreSQL?

[英]Why my code python only can import some data from a file .CSV to my database PostgreSQL?

Description and Objective描述和目标

This is the project 1 of the CS50 Web programing's course.这是CS50 Web编程课程的项目1。 I need to import the content of a table from a file.csv to a table in my database PostgreSQL through Python.我需要通过 Python 将表中的内容从文件 csv 导入我的数据库 PostgreSQL 中的表。 The table has the next format:该表具有以下格式:

isbn,title,author,year
0380795272,Krondor: The Betrayal,Raymond E. Feist,1998

The columns of the table were created directly in my PostgreQSL database with the next data type:表的列是直接在我的 PostgreQSL 数据库中创建的,具有以下数据类型:

id: Integer not null
isbn: Varchar not null
title: Text not null
author: Varchar not null
year: Integer not null

I have the next Python code:我有下一个 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()

Issue问题

When I run the python code to import the data table from the file.csv, the system throws an error:当我运行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>

In order to isolate the problem, I try to import the file.CSV but supriming the first row which include the names of the columns (isbn, title, author, year) and when I run the code, It starts the data transfer but It stops suddenly with another error when It try import a row where the data "title" or "author" contains Double quotes (" ") and Comma (,) .为了隔离问题,我尝试导入文件。CSV 但将包含列名称(isbn,title,author,year)的第一行放在首位,当我运行代码时,它开始数据传输,但它当它尝试导入数据“标题”或“作者”包含双引号(“”)逗号(,)的行时突然停止并出现另一个错误。 For example the next row with the author "VE Schwab, Victoria Schwab" generate that conflict:例如,作者“VE Schwab, Victoria Schwab”的下一行会产生这种冲突:

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

And the new error is like this:新的错误是这样的:

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

The data transfer is finished succesfully when the file.CSV is imported without the first row (isbn, title, author, year) and without data that contains Double quotes (" ") and Commas (,) .当文件.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>

Finally I tried inserting some code lines but the result was the same:最后我尝试插入一些代码行,但结果是一样的:

import psycopg2导入 psycopg2

reader.读者。 next下一个

db.close() 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()

Conclusion I need a help to modify this python code that let me import completly the file.csv including the first row and the data that contains Double Quotes (" ") and Commas (,) .结论我需要帮助来修改这个 python 代码,让我完全导入文件。csv 包括第一行和包含双引号 (" ")逗号 (,)的数据。

reader.__next__

This simply retrieves the method, it does not invoke the method.这只是检索方法,它不调用方法。 You need reader.__next__() , but I think next(reader) might be more conventional.您需要reader.__next__() ,但我认为next(reader)可能更传统。

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

Works fine for me.对我来说很好。 Maybe your actual file has smart quotes or something like that rather than straight ASCII.也许您的实际文件有智能引号或类似的东西,而不是直接的 ASCII。

Try尝试

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

see csv.reader also prior SO answer,请参阅csv.reader 之前的 SO答案,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的 Python 代码仅对某些输入给出错误答案? - Why is my python code giving wrong answers for only some inputs? 试图通过python3将csv文件导入postgresql数据库 - trying to import csv file into postgresql database through python3 为什么我的代码中没有将“流派”数据写入 .csv 文件 - Why are the 'Genre' data not written into the .csv file in my code 为什么我不能将熊猫导入到我的 python 文件中? - Why I can't import panda to my python file? 我需要将 csv 文件中的数据高效地分批导入 postgresql 表和 python - I need to import data from a csv file to a postgresql table with python efficiently and in batches 为什么我的 python 代码返回导入错误? - Why is my python code returning an import error? 无法将ggplot导入我的python代码 - Can't Import ggplot into my python code 为什么我可以使用Python3修改我的网络抓取工具,使它不仅返回Reddit的几张图片和一些重复图片? - Why can I modify my web crawler with Python3 so it doesn't only return a couple of images and some duplicates from reddit? 如何使用sqlite3从csv文件将数据导入python 3.4中的数据库 - How to import data to a database in python 3.4 from a csv file using sqlite3 如何将我的抓取数据从列表导出到csv文件? - How can I export my scraped data from a list to my csv file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM