python 读取 postgreSQL 数据保存CSV文件


self.cursor.copy_expert("COPY (%s) TO STDOUT DELIMITER '`'"%(cmd),open(self.dump_file,"w"))



import psycopg2
import os
import csv
ROOT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)),"..")
CSV_SEP = '`'

class Test():
def __init__(self):
self.database = 'test'
self.user = '###'
self.host = '###'
self.password = '###'
self.conn = psycopg2.connect("dbname='" + self.database + "' user='" + self.user + "' host='" + self.host + "' password='" + self.password + "'")
self.cursor = self.conn.cursor()
self.dump_file = 'test'

def dump_Test(self):
cmd='select * from public."Table1"'
print cmd
f = "%s_navstrand.csv" % (self.dump_file)
self.cursor.copy_expert("COPY (%s) TO STDOUT DELIMITER '`' CSV " % (cmd), open(f, "w"))

def read_csv2(self):
processcount = 0
f = "%s_navstrand" % (self.dump_file)
with open(f, "r", 1024 * 1024 * 1024) as csv_f:
for line in csv_f:
print line
def read_csv(self):
processcount = 0
f = "%s_navstrand.csv" % (self.dump_file)
with open(f) as csvfile:
lines = csv.reader(csvfile,delimiter='`')
for line in lines:
print line
def get_statistic(self):
self.dump_Test()
self.read_csv()

if __name__ == "__main__":
t = Test()
t.get_statistic()

posted @ 2017-07-12 09:32  百变小超  阅读(2480)  评论(0编辑  收藏  举报