简体   繁体   English

在Python中读取从MongoDB导出的CSV文件

[英]Read CSV file that was exported from MongoDB in Python

I am working for hours on loading a CSV file into Python using the well-known pd.read_csv('..') 我花了几个小时使用众所周知的pd.read_csv('..')将CSV文件加载到Python中

However, there is a problem: 但是,有一个问题:

Error message : Error tokenizing data. C error: Expected 3991 fields in line 14, saw 4572

But yes, my code is without mistakes. 但是,是的,我的代码没有错误。

The CSV looks like this.. CSV看起来像这样。

{"_id":{"$oid":"5cf683d88eb9ad12c84f6469"},"ID":"22991137","name":"M. Lundströ 

Maybe the problem occurs because MongoDB is using strict BSON formats, but honestly - I do not know anything about that. 可能是因为MongoDB使用严格的BSON格式而出现问题,但老实说-我对此一无所知。

Does anyone have a solution ? 有没有人有办法解决吗 ?

You can use pd.read_csv() only on a csv file. 您只能在csv文件上使用pd.read_csv()。 However the format looks like invalid JSON to me(parenthesis not closed). 但是该格式对我来说似乎是无效的JSON(括号未关闭)。

You need to export this way for mongodb - 您需要以这种方式为mongodb导出-

mongoexport --db dbname --collection col --type=csv --fields _id,field1,feild2 --out outfile.csv

EDIT: 编辑:

if you want to read the JSON file only, you may read it like this - 如果您只想读取JSON文件,则可以这样阅读-

import json

with open('filepath', 'rb') as f:
    data = json.load(f)
    print(data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM