0
我的python腳本按順序解析文件,並進行簡單的數據清理並寫入新的csv文件。我正在使用csv
。腳本運行時間非常長。瞭解python cProfile輸出
cProfile
輸出如下:
我已經做了很多在這裏張貼問題之前,谷歌搜索。
鏈接到圖像image link
這裏添加代碼,這就是所謂
def db_insert(coCode, bse):
start = time()
q = []
print os.path.join(FILE_PATH, str(bse)+"_clean.csv");
f1 = open(os.path.join(FILE_PATH, str(bse)+"_clean.csv"))
reader = csv.reader(f1)
reader.next()
end = time()
# print end-start
for idx,row in enumerate(reader):
ohlc = {}
date = datetime.strptime(row[0], '%Y-%m-%d')
date = row[0]
row = row[1:6]
(op, high, low, close, volume) = row
ohlc[date] = {}
ohlc[date]['open'] = op
ohlc[date]['high'] = high
ohlc[date]['low'] = low
ohlc[date]['close'] = close
ohlc[date]['volume'] = volume
q.append(ohlc)
end1 = time()
# print end1-end
db.quotes.insert({'bse':str(bse), 'quotes':q})
# print time()-end1
f1.close()
q = []
print os.path.join(FILE_PATH, str(coCode)+".csv");
f2 = open(os.path.join(FILE_PATH, str(bse)+"_clean.csv"))
reader = csv.reader(f2)
reader.next()
for idx,row in enumerate(reader):
ohlc = {}
date = datetime.strptime(row[0], '%Y-%m-%d')
date = row[0]
try:
extra = row[7]+row[8]+row[9]
except:
try:
extra = row[7]
except:
extra = ''
row = row[1:6]
(op, high, low, close, volume) = row
ohlc[date] = {}
ohlc[date]['open'] = op
ohlc[date]['high'] = high
ohlc[date]['low'] = low
ohlc[date]['close'] = close
ohlc[date]['volume'] = volume
ohlc[date]['extra'] = extra
q.append(ohlc)
db.quotes_unadjusted.insert({'bse':str(bse), 'quotes':q})
f2.close()
你能發佈更可讀的截圖嗎? – Gianluca 2014-12-02 22:19:09
在底部添加圖片鏈接 – 2014-12-02 22:22:40
爲什麼只發布圖片,如果您只能複製文本?至於你的腳本爲什麼需要這麼長時間才能運行,那麼如果我們沒有看到腳本/ – Carpetsmoker 2014-12-02 22:48:27