我在postgresql中有一个名为mytable的表格,我需要将这个表格的内容从python应用程序打印到stdout。在python中打印postgresql表格到标准输出
我目前做如下:
conn = psycopg2.connect("dbname=postgres user=postgres password=psswd")
cur = conn.cursor()
cur.copy_to(sys.stdout,'mytable',sep = '\t')
不过,我得到一些“\ n”时,它的一些列之间的印刷英寸我相信之所以发生这种情况是因为在打印过程中,该行超出并进入了psql终端的下一行,因此出现这些\ N。
输出:
E0307 1 M 400 Ethan UTDallas 12.98580404 \N 50.79403657 1
E0307 1 M 400 Lucas Baylor 15.18511175 \N 56.87285183 3
E0307 1 M 400 Jackson Baylor 13.64228411 \N 56.87285183 3
E0307 1 M 400 Jacob Baylor 13.19878974 \N 56.87285183 3
E0307 1 M 400 Samuel Baylor 14.84666623 \N 56.87285183 3
我的问题是:在输出
如何摆脱这些\ n?是否有另一种打印表格的方式?我试图避免我必须执行整个“SELECT * FROM my_table”查询的方法。某些仅使用要打印的表格名称的东西。
另外,如何在打印时获取表格标题?我试过如下:
cur.execute( “COPY MYTABLE与CSV标题STDOUT”)
我收到此错误信息:
ProgrammingError: can't execute COPY TO: use the copy_to() method instead
而且,我不知道如果这是最好的方法。但我试图做:)
我得到这个错误: 回溯(最近通话最后一个): 文件 “”,1号线,在 MY_TABLE = pd.read_sql_table('team_totals ”,连接) 文件 “C:\用户\ peshalnayak \ Anaconda3 \ LIB \站点包\大熊猫\ IO \ sql.py”,线351,在read_sql_table 提高NotImplementedError( “read_sql_table只支持” NotImplementedError :read_sql_table仅支持SQLAlchemy connectable。 –
foobar
好吧我会更新我的答案,使用read_sql函数,其中查询 –
好吧,希望'my_table'或'another_attempt'行工作。一个使用标准的'pd_read_sql',第二个使用'pd.io.sql',我发现它特别建议连接到postgres。好奇它是否有用,或者两者兼而有之。 –