如何使用psycopg2将postgresql的结果保存到csv / excel文件?

编程入门 行业动态 更新时间:2024-10-28 08:20:46
本文介绍了如何使用psycopg2将postgresql的结果保存到csv / excel文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在postgresql中使用 driving_distance 查找所有节点之间的距离,这是我在pyscripter中的python脚本,

I use driving_distance in postgresql to find distances between all nodes, and here's my python script in pyscripter,

import sys #set up psycopg2 environment import psycopg2 #driving_distance module query = """ select * from driving_distance ($$ select gid as id, start_id::int4 as source, end_id::int4 as target, shape_leng::double precision as cost from network $$, %s, %s, %s, %s ) ;""" #make connection between python and postgresql conn = psycopg2.connect("dbname = 'routing_template' user = 'postgres' host = 'localhost' password = '****'") cur = conn.cursor() #count rows in the table cur.execute("select count(*) from network") result = cur.fetchone() k = result[0] + 1 #run loops rs = [] i = 1 while i <= k: cur.execute(query, (i, 1000000, False, False)) rs.append(cur.fetchall()) i = i + 1 #print result for record in rs: print record conn.close()

结果很好,并且在python解释器中的一部分看起来像这样,

The result is fine, and part of the it in python interpreter looks like this,

[(1, 2, 35789.4069722436), (2, 2, 31060.0761437413), (3, 19, 30915.1312550546), (4, 3, 33438.0715007666), (5, 4, 29149.0894812718), (6, 7, 25504.020006665), (7, 7, 29594.741802956), (8, 5, 20736.2427352646), (9, 10, 19545.809601197), (10, 8, 22609.5146670393), (11, 9, 14134.5400189648), (12, 11, 12266.7845493204), (13, 18, 17426.7449057031), (14, 21, 11754.7277029158), (15, 18, 13128.3548040769), (16, 20, 21924.2253916803), (17, 11, 15209.9969992088), (18, 20, 26316.7797545076), (19, 13, 604.414419026164), (20, 16, 740.652673783403), (21, 15, 0.0), (22, 15, 2378.768084459)] [(1, 2, 38168.1750567026), (2, 2, 33438.8442282003), (3, 19, 33293.8993395136), (4, 3, 35816.8395852256), (5, 4, 31527.8575657308), (6, 7, 27882.788091124), (7, 7, 31973.509887415), (8, 5, 23115.0108197236), (9, 10, 21924.577685656), (10, 8, 24988.2827514983), (11, 9, 16513.3081034238), (12, 11, 14645.5526337793), (13, 18, 19805.5129901621), (14, 21, 14133.4957873748), (15, 18, 15507.1228885359), (16, 20, 24302.9934761393), (17, 11, 17588.7650836678), (18, 20, 28695.5478389666), (19, 13, 2983.18250348516), (20, 16, 3119.4207582424), (21, 15, 2378.768084459), (22, 15, 0.0)]

我想将这些结果导出到新的csv或excel文件中,已经查看了这些相关的帖子和​​网站,

I want to export these results to a new csv or excel files, and I have looked these related post and website,

  • PostgreSQL:将结果数据从SQL查询导出到Excel / CSV
  • 保存(postgres)sql输出o csv文件
  • Psycopg 2.5.3。 dev0文档
  • PostgreSQL: export resulting data from SQL query to Excel/CSV
  • save (postgres) sql output to csv file
  • Psycopg 2.5.3.dev0 documentation

但是仍然无法在pyscripter下导出这些文件,我该怎么办?

But still can't export these working under pyscripter, how can I do?

我正在Windows 8.1 x64下使用PostgreSQL 8.4,python 2.7.6。

I am working with postgresql 8.4, python 2.7.6 under Windows 8.1 x64.

更新#1: 我尝试了Talvalin提供的以下代码(谢谢!),

Update#1: I tried the following code provided by Talvalin(thanks!),

import sys #set up psycopg2 environment import psycopg2 #driving_distance module query = """ select * from driving_distance ($$ select gid as id, start_id::int4 as source, end_id::int4 as target, shape_leng::double precision as cost from network $$, %s, %s, %s, %s ) """ #make connection between python and postgresql conn = psycopg2.connect("dbname = 'TC_routing' user = 'postgres' host = 'localhost' password = '****'") cur = conn.cursor() outputquery = 'copy ({0}) to stdout with csv header'.format(query) with open('resultsfile', 'w') as f: cur.copy_expert(outputquery, f) conn.close()

但是下面有错误,

>>> Traceback (most recent call last): File "C:/Users/Heinz/Desktop/python_test/driving_distance_loop_test.py", line 27, in <module> cur.copy_expert(outputquery, f) ProgrammingError: 錯誤: 在"語法錯誤"附近發生 % LINE 10: $$, %s, %s, %s, %s ^

也许我需要在上面的代码中添加更多内容。

Maybe I need to add something more in the code above.

推荐答案

基于 Psycopg2的cursor.copy_expert()和 Postgres COPY 文档和原始代码示例,请尝试一下。我在笔记本电脑上测试了类似的查询导出,因此我有足够的信心可以解决此问题,但是请让我知道是否存在任何问题。

Based on Psycopg2's cursor.copy_expert() and Postgres COPY documentation and your original code sample, please try this out. I tested a similar query export on my laptop, so I'm reasonably confident this should work, but let me know if there are any issues.

import sys #set up psycopg2 environment import psycopg2 #driving_distance module #note the lack of trailing semi-colon in the query string, as per the Postgres documentation query = """ select * from driving_distance ($$ select gid as id, start_id::int4 as source, end_id::int4 as target, shape_leng::double precision as cost from network $$, %s, %s, %s, %s ) """ #make connection between python and postgresql conn = psycopg2.connect("dbname = 'routing_template' user = 'postgres' host = 'localhost' password = 'xxxx'") cur = conn.cursor() outputquery = "COPY ({0}) TO STDOUT WITH CSV HEADER".format(query) with open('resultsfile', 'w') as f: cur.copy_expert(outputquery, f) conn.close()

更多推荐

如何使用psycopg2将postgresql的结果保存到csv / excel文件?

本文发布于:2023-10-23 18:39:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1521686.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   文件   postgresql   csv   excel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!