如何根据日期使用Python将数据导入多个CSV文件

时间:2018-12-26 08:31:18

标签: python postgresql psycopg2

我创建了以下代码,用于从PostgreSQL DB导入CSV文件中的数据。但是,我想根据日期创建多个文件。

alias

文件的拆分方式应类似于以下格式:

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()

1 个答案:

答案 0 :(得分:0)

每当行日期更改时,您都可以遍历查询结果并打开一个新文件。结果必须按日期排序,否则可能会丢失一些数据。

final TextView averageView = findViewById(R.id.averageView); 
final String averageText = getString(R.string.average); 
final Button calculateButton = findViewById(R.id.calculateAverageButton); 

calaculateButton.setOnClickListener(new View.onClickListener() { 
    @SupressLint("SetTextI18n")                                                     
    public void onClick(View v) { 

        double grade[]  = {Double.parseDouble(((EditText) findViewById(R.id.grade1)).getText().toString()); 
        double weight[] = {Double.parseDouble(((EditText) findViewById(R.id.weight1)).getText().toString()); 
        double weightTotal = weight[0]; double sum = grade[0] * weight[0] 
        double average = sum / weightTotal 
        averageView.setText(averageText + " " + Double.toString(average));

上述解决方案对于相当小的数据集是可以接受的。如果一天的数据量很大,您应该使用copy_expert()

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()