【问题标题】:Python PostgresQL - export to CSV not exporting all linesPython PostgreSQL - 导出到 CSV 不导出所有行
【发布时间】:2019-10-16 10:49:21
【问题描述】:

我有一个 Postgresql SQL 查询,我想用 Python 运行并将它导出到 CSV 文件。

我对 Python 很陌生,但我设法编写了一个脚本,可以运行查询并导出到文件。

import psycopg2

# File path and name.
fileName = 'test.csv'


# Database connection variable.
connect = None

# Check if the file path exists.
if os.path.exists(filePath):

    try:

        # Connect to database.
        connect = psycopg2.connect(host="xxxx", port="5439", database="xxxx", user="xxxx", password="xxxx")

    except psycopg2.DatabaseError as e:

        # Confirm unsuccessful connection and stop program execution.
        print("Database connection unsuccessful.")
        quit()

    # Cursor to execute query.
    cursor = connect.cursor()

    # SQL to select data from the person table.
    sqlSelect = """
SELECT * FROM TABLE
                """

    try:

        # Execute query.
        cursor.execute(sqlSelect)

        # Fetch the data returned.
        results = cursor.fetchall()

        # Extract the table headers.
        headers = [i[0] for i in cursor.description]

        #Print the results
        #print(pd.read_sql(sqlSelect, connect))
        print(tb.tabulate(results, headers=headers, tablefmt='psql', showindex="always", floatfmt=".10f"))

        # Open CSV file for writing.
        csvFile = csv.writer(open(filePath + fileName, 'w', newline=''),
                             delimiter=',', lineterminator='\r\n',
                             quoting=csv.QUOTE_ALL, escapechar='\\')

        # Add the headers and data to the CSV file.
        csvFile.writerow(headers)

        for row in results:
            csvFile.writerow(row)

        # Message stating export successful.
        print("Data export successful.")

        # csvFile.close()

    except psycopg2.DatabaseError as e:

        # Message stating export unsuccessful.
        print("Data export unsuccessful.")
        quit()

    finally:

        # Close database connection.
        cursor.close()
        connect.close()

else:

    # Message stating file path does not exist.
    print("File path does not exist.")


cursor.close()
connect.close()

我运行的查询生成了 70 行结果(当我通过数据库程序运行它时)。但是,当我将数据导出到 CSV 时,它只导出 48 行。

我不知道出了什么问题。

【问题讨论】:

  • 可能是因为您没有关闭 csvFile?无论如何,我建议在 psycopg2 的游标类中使用内置的 copy_to。它将为您创建 csv 文件。
  • 谢谢!好地方。我使用了with 语句,它在导出数据后关闭文件。

标签: python python-3.x postgresql csv export-to-csv


【解决方案1】:

你的代码在 postgres 中有 490 行对我有用。

也许您的问题在于数据库中的某些记录具有某些特殊字符,导致代码停止并且无法获得预期结果。

调试您的脚本,添加异常打印以检查是否是问题所在。

【讨论】:

  • 我打印了结果,它有所有 70 行,但导出工作不正常。杰里米有办法。感谢您抽出宝贵时间回答我的问题。
  • 不客气,很高兴你能用 Jeremy 的解决方案解决你的问题
猜你喜欢
  • 1970-01-01
  • 2020-08-03
  • 1970-01-01
  • 1970-01-01
  • 2018-09-11
  • 2013-09-02
  • 2021-12-02
  • 2022-01-21
相关资源
最近更新 更多