【问题标题】:Creating a custom json from existing json从现有 json 创建自定义 json
【发布时间】: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\"

    },
    ]

    标签: python json


    【解决方案1】:

    试试(json1 包含您问题中的数据):

    import re
    
    out = []
    for d in json1["blocks"]:
        num = re.search(r"^(\d+\.)\s*(.*)", d["text"].replace("\n", ""))
    
        out.append(
            {
                "key": num.group(1) if num else "",
                "text": (num.group(2) if num else d["text"]).rsplit(maxsplit=1)[0],
            }
        )
    
    print(out)
    

    印刷:

    [
        {"key": "", "text": "Contents"},
        {"key": "1.", "text": "Completed Activities & Accomplishments"},
        {"key": "2.", "text": "Planned Activities for Next Reporting Period"},
    ]
    

    【讨论】:

      【解决方案2】:

      另一种简单的方法是(如果您的第一个列表称为“Mylist”:

      new_list =[]
      
      for dic in Mylist:
          new_list.append({'key':dic['key'],
                           'text':dic['text']})
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-08-14
        • 2023-03-26
        • 1970-01-01
        • 2013-12-26
        • 1970-01-01
        • 2022-02-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多