2017-03-10 90 views
0

我正在使用mongoimport導入包含某些日期字段的CSV文件。日期格式爲「DD.MM.YYYY」。Mongoimort - 從CSV文件導入日期字段

嘗試導入文件時,出現錯誤消息。

失敗:在文檔#0類型強制失敗的列「ImportedDate」,無法分析令牌'16 .08.2015' 鍵入日期

回答

1

你必須改變你的日期格式,以適應mongodb所需的格式。貝婁舉例說明如何在python中做到這一點:

from datetime import datetime 
import csv 
import numpy as np; 

file = "your_file.csv" 

outCsv = [] 
header = ['header1','header2',...,'headerN'] 
outCsv.append(header) 

with open(file,'r') as csvfile: 
    reader = csv.DictReader(csvfile) 
    for row in reader: 
     d = datetime.strptime(''.join(row['dateHeader'].rsplit(':', 1)), '%Y.%m.%d') 
     iso_string = d.strftime('%Y-%m-%dT%H:%M:%S%z') 
     tmpLine = [row['header1-value'],...,iso_string,row['headerN-value']] 
     outCsv.append(tmpLine) 


np.savetxt("file_to_import.csv",outCsv,delimiter=",", fmt="%s") 

希望我的回答很有幫助。