Я пытаюсь преобразовать файл json в pandas df.Этот файл JSON имеет дубликаты ключей.
После ответа на этот вопрос: Анализатор Python json позволяет дублировать ключи , я пытался сделать:
from collections import OrderedDict
from json import JSONDecoder
def make_unique(key, dct):
counter = 0
unique_key = key
while unique_key in dct:
counter += 1
unique_key = '{}_{}'.format(key, counter)
return unique_key
def parse_object_pairs(pairs):
dct = OrderedDict()
for key, value in pairs:
if key in dct:
key = make_unique(key, dct)
dct[key] = value
return dct
decoder = JSONDecoder(object_pairs_hook=parse_object_pairs)
with open("file.json") as f:
obj = decoder.decode(f)
#print obj
Я получил следующую ошибку:
TypeError
Traceback (most recent call last)
<ipython-input-70-0d2633348c10> in <module>()
2
3 with open("file.json") as f:
----> 4 obj = decoder.decode(f)
5 #print obj
6
C:\ProgramData\Anaconda2\lib\json\decoder.pyc in decode(self, s, _w)
362
363 """
--> 364 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
365 end = _w(s, end).end()
366 if end != len(s):
Ошибка типа: ожидаемая строка или буфер "
Чего мне не хватает?