【发布时间】:2022-09-30 21:32:28
【问题描述】:
我有类似下面的 Json
输入Json
\"blocks\": [
{
\"key\": \"\",
\"text\": \"Contents\",
\"type\": \"unstyled\",
\"depth\": 0,
\"inlineStyleRanges\": [
{
\"offset\": 0,
\"length\": 8,
\"style\": \"BOLD\"
}
],
\"entityRanges\": [],
\"data\": {}
},
{
\"key\": \"\",
\"text\": \"1.\\u00a0\\u00a0\\u00a0\\u00a0 Completed\\n Activities & Accomplishments\\u00a0 1\",
\"type\": \"unstyled\",
\"depth\": 0,
\"inlineStyleRanges\": [],
\"entityRanges\": [
{
\"offset\": 0,
\"length\": 49,
\"key\": 0
}
],
\"data\": {}
},
{
\"key\": \"\",
\"text\": \"2.\\u00a0\\u00a0\\u00a0\\u00a0 Planned Activities for\\n Next Reporting Period\\u00a0 3\",
\"type\": \"unstyled\",
\"depth\": 0,
\"inlineStyleRanges\": [],
\"entityRanges\": [
{
\"offset\": 0,
\"length\": 55,
\"key\": 1
}
],
\"data\": {}
},
我正在尝试提取“文本”键数据并希望将其转换为键值格式的新 json 我现在可以正确提取文本我只想知道如何成功地将数字与字母分开
def jsontojson():
with open(\'C:\\Extraction\\Docs\\TMS TO 692M15-22-F-00073 Monthly Status _ Financial Report July 2022.json\') as json_file:
# json1=json_file.read()
json1=json.load(json_file)
value=\"\"
for dict1 in json1[\"blocks\"]:
# print(dict1)
for key in dict1:
if key==\"text\":
value+=dict1[key]
dict2={}
d=value.split()
print(\"Value of d\",d)
if str(d).isdigit():
dict2[\'key1\']=d
else:
dict2[\'desciption\']=d
print(\"Dictionary is\",dict2[\'key\'])
对于上面的代码,它给了我关键错误:key1
让我知道我错在哪里或我需要做什么才能获得输出
预期输出
[
{
\"key\": \"\",
\"text\": \"Contents\"
},
{
\"key\": \"1.\",
\"text\": \"Completed Activities & Accomplishments\"
},
{
\"key\": \"2.\",
\"text\": \"Planned Activities for Next Reporting Period\"
},
]