提高CSV文件写入速度

时间:2018-06-06 23:48:56

标签: python performance csv

下面的代码工作得很好。我想知道有没有办法提高它的写入速度。因为编写.csv文件目前需要12到20秒。

import csv
import os

data = [...1000 item list, where each item is a dictionary with 8 values...]

for candle in data:
        if os.path.isfile('data'+os.sep+i+os.sep+coin_symbol+'.csv'):
            H = []
            H.append(candle['timestamp'])
            H.append(candle['open'])
            H.append(candle['close'])
            H.append(candle['min'])
            H.append(candle['max'])
            H.append(candle['volume'])
            H.append(candle['volumeQuote'])

            with open('data'+os.sep+i+os.sep+coin_symbol+'.csv', 'a', newline="") as csvfile:
                csvwriter = csv.writer(csvfile, delimiter=',')
                csvwriter.writerow(H)
        else: 

                H = []
                H.append('TimeStamp')
                H.append('Open')
                H.append('Close')
                H.append('Min')
                H.append('Max')
                H.append('Volume')
                H.append('VolumeQuote')



                with open('data'+os.sep+i+os.sep+coin_symbol+'.csv', 'w', newline="") as csvfile:
                    csvwriter = csv.writer(csvfile, delimiter=',')
                    csvwriter.writerow(H)
                H = []
                H.append(candle['timestamp'])
                H.append(candle['open'])
                H.append(candle['close'])
                H.append(candle['min'])
                H.append(candle['max'])
                H.append(candle['volume'])
                H.append(candle['volumeQuote'])

                with open('data'+os.sep+i+os.sep+coin_symbol+'.csv', 'a', newline="") as csvfile:
                    csvwriter = csv.writer(csvfile, delimiter=',')
                    csvwriter.writerow(H)

对于Else声明,有没有办法以不同的方式编写它?

1 个答案:

答案 0 :(得分:0)

以下方法可避免重新打开输出文件。它还使用DictWriter作为data已经是字典:

import os
import csv


filename = os.path.join('data', i, coin_symbol + '.csv')
mode = 'a' if os.path.isfile(filename) else 'w'

fieldnames = ['timestamp', 'open', 'close', 'min', 'max', 'volume', 'volumeQuote']
header = ['TimeStamp', 'Open', 'Close', 'Min', 'Max', 'Volume', 'VolumeQuote']

with open(filename, mode, newline='') as f_output:
    csv_output = csv.DictWriter(f_output, fieldnames=fieldnames)

    if mode == 'w':
        csv_output.writerow(dict(zip(fieldnames, header)))

    for row in data:
        csv_output.writerow(row)

os.path.join()是一种比尝试添加os.sep更好的方法。