Process Special Json With Keys As Numbers
I want to extract data from file into a dictionary via json.loads. Example: {725: 'pitcher, ewer', 726: 'plane, carpenter's plane, woodworking plane'} json.loads can't handle the
Solution 1:
The file you supplied seems like a valid Python dict
, so I suggest an alternative approach, with literal_eval
.
from ast import literal_eval
data = literal_eval(r.text)
print(data[726])
Output: plane, carpenter's plane, woodworking plane
If you still like json
, then you can try replacing the numbers with strings using regex.
import re
s = re.sub(r"(?m)^(\W*)(\d+)\b", r'\1"\2"', r.text)
data = json.loads(s)
Post a Comment for "Process Special Json With Keys As Numbers"