使用熊猫规范化方法的莫名其妙的键错误



以下代码

import pandas as pd
dic = {'_id': '5436e3abbae478396759f0cf', 'meta': {'clinical': {'benign_malignant': 'benign', 'age_approx': 55, 'sex': 'female', 'diagnosis': 'nevus', 'diagnosis_confirm_type': None, 'anatom_site_general': 'anterior torso', 'melanocytic': True}, 'acquisition': {'image_type': 'dermoscopic', 'pixelsX': 1022, 'pixelsY': 767}}, 'name': 'ISIC_0000000'}
frame = pd.io.json.json_normalize(dic)

抛出一个

KeyError: 'diagnosis_confirm_type'

我正在使用熊猫版本0.23.0。代码在版本0.22.0中工作没有错误。

更新:

显然,0.23.0中确实存在导致此问题的错误。见 https://github.com/pandas-dev/pandas/pull/21164

如果你最初把它作为一个字符串,你甚至不需要正则表达式:

validPJson = [line.replace('None', '"None"').replace('True', '"True"') for line in invalidJsonObjects]

请参阅此处了解为什么它比正则表达式更好:使用 Python 的 string.replace vs re.sub

编辑:从评论中,我了解到您的问题是加载该格式的文件而没有先修复它,这就是为什么您在加载时遇到错误的原因(顺便说一句,这些错误应该真的在您的问题中,否则您只是混淆了很多人试图提供帮助(。

我的建议,先用类似的方法修复文件:

with open(pathToFile, 'r') as fp:
contents = fp.read()
with open(pathToFile, 'w') as fp:
fp.write(contents.replace('None', '"None"').replace('True', '"True"'))

只有在之后,才尝试使用json读取文件,看看是否有效

最新更新