感谢您在我的问题上的帮助。

我想将响应查询附加到现有的CSV文件中。我通过this example实现了此功能,但出于未知原因-输出文件保持为空。

这是我的代码的最小片段:

import psycopg2
import numpy as np

# Connect to an existing database
conn = psycopg2.connect(dbname="...", user="...", password="...", host="...")
# Open a cursor to perform database operations
cur = conn.cursor()

f = open('file_name.csv', 'ab') # "ab" for appending

cur.execute("""select * from table limit 10""") # I have another query here but it isn't relevant.
cur_out = np.asarray(cur.fetchall())


到这里为止,它运作完美。当我print(cur_out)时,我得到了期望的输出。但在下一步中:

np.savetxt(f, cur_out, delimiter=",", fmt='%s')


该文件为空,我没有找到原因。

你能帮我吗?

感谢帮助者。

最佳答案

我不知道该怎么说但是您的代码非常适合我。

我的建议:


如果您使用此名称已有文件,请尝试将其删除并重新执行代码,
尝试更改批次大小。
尝试将文件名的后缀更改为txt或dat。


最好的祝愿。

10-06 10:33
查看更多