问题描述
以下命令序列运行良好,并生成与 sql 表匹配的 DataFrame:
copy_sql = "copY mytable TO STDOUT WITH CSV HEADER"
conn = myengine.raw_connection()
cur = conn.cursor()
store = io.StringIO()
cur.copy_expert(copy_sql,store)
store.seek(0)
# this is for debugging
# it correctly outputs the CSV string from STDOUT
print(store.read())
store.seek(0)
cur.close()
# this works
df = pd.read_csv(store)
但是,我试图将 copY
命令的输出传递给 gzip
,然后将 gzip
输出传递给 STDOUT
。以下结果会导致 pandas.errors.EmptyDataError: No columns to parse from file
错误。
copy_sql = "copY mytable TO PROGRAM 'gzip -f --stdout' WITH CSV HEADER"
conn = myengine.raw_connection()
cur = conn.cursor()
store = io.StringIO()
cur.copy_expert(copy_sql,store)
store.seek(0)
# this is for debugging
# it should output the compressed string,# but actually outputs nothing
print(store.read())
store.seek(0)
cur.close()
# this doesn't work as Pandas finds nothing in `store`
df = pd.read_csv(store,compression="gzip")
在终端中尝试 echo "hey" | gzip -f --stdout
正确地将压缩字符串输出到 STDOUT,我相信 TO PROGRAM 'gzip -f --stdout'
将等同于 TO STDOUT
,只是发送到 STDOUT 的输出将被压缩,但显然有些东西不见了。
conn
连接通过网络连接到远程机器上的 Postgresql 数据库。
我真正的目标是在通过网络之前压缩 CSV 输出,然后从压缩字符串中获得 Pandas read_csv
。任何其他方式来实现这一点将不胜感激。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)