所以虽然我一直没能找到答案。如果需要,有一个解决方法。出于某种原因,当您通过 pymongo 从数据帧导入这些文件时,该管道似乎没有任何错误。
基本上,在需要的地方获取数据的工作是这样的:
path = "/home/linux/Downloads/csvs2import"
dir_list = os.listdir(path)
dtypes = {'CountryCode': str,
'date': str,
'SQLDATE': str,
'ActionGeo_ADM1Code': str,
'lat': np.float64,
'long': np.float64,
'URL': str,
'sentiment': np.float64,
'GoldsteinScale': np.float64,
'EventCode': str,
'EventBaseCode': str,
'EventRootCode': str,
'QuadClass': str,
'Actor1Code': str,
'Actor1Name': str,
'Actor1EthnicCode': str,
'Actor1Religion1Code': str,
'Actor1Religion2Code': str,
'Actor1Geo_Fullname': str,
'Actor1Type1Code': str,
'Actor2Code': str,
'Actor2Name': str,
'Actor2EthnicCode': str,
'Actor2Religion1Code': str,
'Actor2Religion2Code': str,
'Actor2Geo_Fullname': str,
'Actor2Type1Code': str,
'NumSources': np.int32}
for f in dir_list:
print(f)
fp = path + '/' + f
data = pd.read_csv(fp, header=0, dtype=dtypes)
collection.insert_many(data.to_dict('records'))
del data
这指定了位于单个文件夹(失败的文件夹)中的 csvs,然后指定了数据类型,没有数据类型它会抛出错误,因为事件代码之类的东西是分类变量,是不明确的;所以我将它们作为字符串导入(例如,050 是一个代码,因此它被导入为“050”而不是 50)。