首页 > 解决方案 > Python PostgreSQL - 导出到 CSV 不导出所有行

问题描述

我有一个 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 行。

我不知道出了什么问题。

标签: pythonpython-3.xpostgresqlcsvexport-to-csv

解决方案


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

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

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


推荐阅读