0

我正在使用 Jupyter 笔记本来处理一些带有 postgresql 的数据库我有以下实例:

import pandas as pd
import (other packages)
conn_string= % I can't show this, but this is ok
conn = psycopg2.connect(conn_string)
cursor=conn.cursor
query= """ copy (select col1,col2 from Table where col3=a_parameter
           and col4=b_parameter) to '/tmp/test.csv' with csv """
pd.read_sql(query,conn)

但我得到了这个错误:

**ProgrammingError: syntax error at or near "("
LINE 1: COPY (select col1,col2 from Table where col3...**
             ^

为什么复制句子有错误? 我正在使用 Postresql 8.0.2

4

2 回答 2

1

像这样的东西:

import csv
            my_file_csv =  my_folder + "\Report_Trip_Day_" + my_opr + "_" + my_local_database + ".csv"


            out = csv.writer(open(my_file_csv, "w", newline=''), delimiter=',', quoting=csv.QUOTE_ALL)
            out.writerow(colnames)
            for row in my_xls_report_table:
                out.writerow(row)
于 2018-06-08T00:13:26.550 回答
0

你可以这样做:

query= """ copy (select col1,col2 from Table where col3=a_parameter
       and col4=b_parameter) """

df=pd.read_sql(query,con=conn)
df.to_csv("name.csv",sep=",")
于 2019-07-10T20:40:29.200 回答