Python 将JSON转换为CSV时出错
转换为csv时出现错误 以下是json数据:Python 将JSON转换为CSV时出错,python,json,Python,Json,转换为csv时出现错误 以下是json数据: "[{" "test_result" ":" "{\"" 1\ "": { \ "" meta_data\ "": { \ "" grill_type\ "": \ ""
"[{"
"test_result"
":"
"{\""
1\ "": {
\
""
meta_data\ "": {
\
""
grill_type\ "": \ ""
Propane\ "",
\""
is_frozen\ "": \ ""
false\ "",
\""
item_material\ "": \ ""
Hotdog\ ""
},
\""
sample_item_index\ "": 1,
\""
survey_result\ "": {
\
""
guess_grill_correct\ "": \ ""
true\ "",
\""
thumbs_up_score\ "": \ ""
0.4\ ""
}
}, \""
10\ "": {
\
""
meta_data\ "": {
\
""
grill_type\ "": \ ""
Propane\ "",
\""
is_frozen\ "": \ ""
true\ "",
\""
item_material\ "": \ ""
Hotdog\ ""
},
\""
sample_item_index\ "": 10,
\""
survey_result\ "": {
\
""
guess_grill_correct\ "": \ ""
true\ "",
\""
thumbs_up_score\ "": \ ""
1.0\ ""
}
},
我正在尝试这个:
df=pd.read\u json(“SheetC.json”,lines=True,encoding=“utf-8-sig”)
错误:
ValueError回溯(最近一次调用)
在()
---->1 df=pd.read_json(“SheetC.json”,lines=True,encoding=“utf-8-sig”)
6帧
/usr/local/lib/python3.7/dist-packages/pandas/io/json//u json.py in\u parse\u no\u numpy(self)
1117如果方向=“列”:
1118 self.obj=数据帧(
->1119个加载(json,precise\u float=self.precise\u float),dtype=None
1120 )
1121 elif orient==“分割”:
ValueError:解码数组值(2)时发现意外字符
我认为问题在于json数据有一些语法错误。首先删除所有转义字符并清理json数据。它最终看起来应该类似于以下内容。之后,您的read_json调用可能会正常运行
[
{
"test_result": {
"1": {
"meta_data": {
"grill_type": "Propane",
"is_frozen": "false",
"item_material": "Hotdog"
},
"sample_item_index": 1,
"survey_result": {
"guess_grill_correct": "true",
"thumbs_up_score": "0.4"
}
},
"10": {
"meta_data": {
"grill_type": "Propane",
"is_frozen": "true",
"item_material": "Hotdog"
},
"sample_item_index": 10,
"survey_result": {
"guess_grill_correct": "true",
"thumbs_up_score": "1.0"
}
}
}
}
]
为什么您的“json数据”看起来像这样?看起来它被处理成了废话。为什么要使用
r
和rstudio
标记?你似乎在使用Python…这绝对不是一个有效的json数据,我想你已经得到了一些很好的提示;这个JSON格式不好。我认为,如果我正确地遵循了结构,这个错误至少给出了一些提示。正如您所发布的,有一个开头[
(表示数组),但没有]
结束数组。此外,在数组中的第二个元素后面还有一个杂散的,
。这也会带来悲伤@schilli在他的帖子中暗示了这一点。如何清理文件?您是否有某些代码会有所帮助?您最初是如何获得这种格式的json的?如果你能将这些信息添加到问题中,可能会有更好的方法以适当的格式获取这些信息。