在 python 中将 postgresql table 打印到标准输出
Print a postgresql table to standard output in python
我在 postgresql 中有一个名为 mytable 的 table,我需要将此 table 的内容从 python 应用程序打印到标准输出。
我目前正在做以下事情:
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?是否有其他打印 table 的方法?我试图避免必须执行整个 "SELECT * FROM my_table" 查询的方式。仅使用要打印的 table 名称的内容。
此外,如何在打印时得到 table headers?我尝试了以下方法:
cur.execute("COPY mytable TO STDOUT with csv header")
我收到此错误消息:
ProgrammingError: can't execute COPY TO: use the copy_to() method instead
另外,我不确定这是否是最好的方法。但是我尝试做的事情:)
没有方便的 postgres table 来测试这个,但这对你有用吗?
import psycopg2 as pg
import pandas as pd
import pandas.io.sql as psql
connection = pg.connect("dbname=postgres user=postgres password=psswd")
#my_table = pd.read_sql_table('table_name', connection)
my_table = pd.read_sql('select * from my-table-name', connection)
another_attempt= psql.read_sql("SELECT * FROM my-table-name", connection)
print(my_table)
# OR
print(another_attempt)
\N
是 null
值的默认文本表示。它可以用 null
parameter of copy_to
改变
要在输出中使用 headers copy_expert
copy = "copy mytable to stdout with csv header delimiter '\t' null 'NULL'"
cursor.copy_expert(copy, sys.stdout)
正如 Neto 先前所说:cur.copy_expert("sql statement", sys.stdout)
会起作用。要使用 copy_to
你需要传递 null 参数。
如果您选择 copy_to
方法(设置空值 - 请参阅文档),请尝试此操作。先打印列名。
header = [i[0] for i in cur.description
print header
cur.copy_to(sys.stdout, 'table', sep='\t', null='\N')
我在 postgresql 中有一个名为 mytable 的 table,我需要将此 table 的内容从 python 应用程序打印到标准输出。
我目前正在做以下事情:
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?是否有其他打印 table 的方法?我试图避免必须执行整个 "SELECT * FROM my_table" 查询的方式。仅使用要打印的 table 名称的内容。
此外,如何在打印时得到 table headers?我尝试了以下方法:
cur.execute("COPY mytable TO STDOUT with csv header")
我收到此错误消息:
ProgrammingError: can't execute COPY TO: use the copy_to() method instead
另外,我不确定这是否是最好的方法。但是我尝试做的事情:)
没有方便的 postgres table 来测试这个,但这对你有用吗?
import psycopg2 as pg
import pandas as pd
import pandas.io.sql as psql
connection = pg.connect("dbname=postgres user=postgres password=psswd")
#my_table = pd.read_sql_table('table_name', connection)
my_table = pd.read_sql('select * from my-table-name', connection)
another_attempt= psql.read_sql("SELECT * FROM my-table-name", connection)
print(my_table)
# OR
print(another_attempt)
\N
是 null
值的默认文本表示。它可以用 null
parameter of copy_to
要在输出中使用 headers copy_expert
copy = "copy mytable to stdout with csv header delimiter '\t' null 'NULL'"
cursor.copy_expert(copy, sys.stdout)
正如 Neto 先前所说:cur.copy_expert("sql statement", sys.stdout)
会起作用。要使用 copy_to
你需要传递 null 参数。
如果您选择 copy_to
方法(设置空值 - 请参阅文档),请尝试此操作。先打印列名。
header = [i[0] for i in cur.description
print header
cur.copy_to(sys.stdout, 'table', sep='\t', null='\N')