2017-03-10 91 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") 

希望我的回答很有帮助。