python - 如何使用基于日期的 Python 将数据导入多个 CSV 文件
问题描述
我创建了以下代码以从 PostgreSQL DB 导入 CSV 文件中的数据。但是,我想根据日期创建多个文件。
import psycopg2
import csv
conn_string = "host='' port='5432' user='' password='' dbname=''"
conn = psycopg2.connect(conn_string)
cur=conn.cursor()
query="select * from sample where date between '' and ''"
cur.execute(query)
title=[i[0] for i in cur.description]
result=cur.fetchall()
csvfile=open('filename.csv','w')
if result:
c = csv.writer(csvfile)
c.writerow(title)
c.writerows(result)
cur.close()
conn.close()
文件应按以下格式拆分:
01jan.csv
02jan.csv
etc.
解决方案
您可以遍历查询结果并在行日期更改时打开一个新文件。结果必须按日期排序,否则可能会丢失一些数据。
import psycopg2
import psycopg2.extras
import csv
import datetime
# conn_string = ...
conn = psycopg2.connect(conn_string)
# we need results in dict
cur = conn.cursor(cursor_factory = psycopg2.extras.DictCursor)
# order by date - important!
query = "select * from sample where date between '2018-01-01' and '2018-01-10' order by date"
cur.execute(query)
title = [i[0] for i in cur.description]
date = None
writer = None
csvfile = None
for row in cur:
if date != row['date']:
# when date changes we should close current file (if opened)
# and open a new one with name based on date
if csvfile:
csvfile.close()
date = row['date']
filename = date.strftime("%d%b")+ '.csv'
csvfile = open(filename, 'w', newline='')
writer = csv.writer(csvfile)
writer.writerow(title)
writer.writerow(row)
cur.close()
conn.close()
上述解决方案对于相当小的数据集是可以接受的。如果一天的数据量很大,你宁愿使用copy_expert()
cur = conn.cursor()
# example loop for ten days of Jan 2018
for day in range(1, 10):
date = datetime.date(2018, 1, day)
filename = date.strftime("%d%b")+ '.csv'
command = 'copy (select * from sample where date = %s) to stdout with csv header'
sql = cur.mogrify(command, [date])
with open(filename, 'w') as file:
cur.copy_expert(sql, file)
推荐阅读
- utf-8 - lua,截断包含 utf-8 编码字符的字符串
- android - 如何在 Web View Android 中嵌入 iframe 视频?
- rust - 为什么在从字符串解析后比较一个值时会出现类型不匹配的错误?
- java - 如果未提供标志,我如何关闭/不启动 spring 应用程序?
- function - Lua:从函数中获取函数之外的局部变量
- sql - Partitioned by 给我创建外部表时重复的错误列
- c++ - 为什么 '€' == '\€' 但 "€" != "\€" 和 u8"€" != u8"\€"
- c# - 多线程应用程序中的单个 DbContext 实例
- java - 如何在 OpenGL 中围绕 Z 轴中心旋转对象?
- javascript - 在 vue js 客户端中存储 api 配置的正确方法