在Python中使用glob结果COPY到Postgres

时间:2013-07-28 17:24:01

标签: python postgresql psycopg2 glob

我正在尝试创建一个将glob结果复制到postgres的python脚本。这就是我所拥有的:

import psycopg2, glob

sitesizetemp = glob.glob('C:/Data/Sheltered BLPUs/CSVs/sitesize*.csv')

conn = psycopg2.connect("dbname=postgres user=postgres")

cur = conn.cursor()

cur.execute("COPY sitesize FROM" sitesizetemp "DELIMITER ',' CSV;")

conn.commit()

cur.close()

conn.close()

我对这一切都很陌生,所以非常欢迎任何帮助!感谢

1 个答案:

答案 0 :(得分:0)

import psycopg2, glob

sitesizetemp = glob.glob('C:/Data/Sheltered BLPUs/CSVs/sitesize*.csv')
conn = psycopg2.connect("dbname=postgres user=postgres")
cur = conn.cursor()

for f in sitesizetemp:
    cur.execute("COPY sitesize FROM '%s' DELIMITER ',' CSV;" % f)

conn.commit()
cur.close()
conn.close()