【问题标题】:How to get rid of "\n" and " ' ' " in my json file如何摆脱我的 json 文件中的“\n”和“''”
【发布时间】:2021-05-19 03:51:11
【问题描述】:

感谢阅读 由于我正在使用的 API,我正在创建一个 json 文件。 我的问题是结果中包含 \h 和 '' 并且 .json 文件不处理 \n 但保留它们,因此文件出现错误。

这是我的代码,它是python:

json_arr = []
for text in gtm_Q6_df['ANSWER']:
    response = natural_language_understanding.analyze(
        text=text,language = 'en',
            features=Features(
            categories=CategoriesOptions(limit=3),
            )).get_result()
    #print("Input text: ",text)
    #print(json.dumps(response, indent=2))
    
    json_data = (json.dumps(response, indent=2))
    json_arr.append (json_data)
    with open('data.json','w') as outline: #with open('data.json','w') as outline:
        json.dump(json_arr,outline)
    
print("break")
print(json_arr)

['{\n  "usage": {\n    "text_units": 1,\n    "text_characters": 113,\n    "features": 1\n  },\n  "language": "en",\n  "categories": [\n    {\n      "score": 0.734718,\n      "label": "/business and industrial/advertising and marketing/marketing"\n    },\n    {\n      "score": 0.675452,\n      "label": "/business and industrial/business operations/management/business process"\n    },\n    {\n      "score": 0.620496,\n    
  "label": "/business and industrial/advertising and marketing/brand management"\n    }\n  ]\n}', '{\n  "usage": {\n    "text_units": 1,\n    "text_characters": 215,\n    "features": 1\n  },\n  "language": "en",\n  "categories": [\n    {\n      "score": 0.913644,\n      "label": "/technology and computing/operating systems"\n    },\n    {\n      "score": 0.855434,\n      "label": "/technology and computing/hardware/computer"\n    },\n    {\n      "score": 0.842841,\n      "label": "/technology and computing/hardware/computer components/chips and processors"\n    }\n  ]\n}', '{\n  "usage": {\n    "text_units": 1,\n    "text_characters": 14,\n    "features": 1\n  },\n  "language": "en",\n  "categories": [\n    {\n      "score": 0.885687,\n      "label": "/automotive and vehicles/cars/hybrid"\n    },\n    {\n      "score": 0.821966,\n     
 "label": "/automotive and vehicles/electric vehicles"\n    },\n    {\n      "score": 0.77387,\n      "label": "/shopping/retail"\n    }\n  ]\n}', '{\n  "usage": {\n    "text_units": 1,\n    "text_characters": 86,\n    "features": 1\n  },\n  "language": "en",\n  "categories": [\n    {\n      "score": 0.967686,\n      "label": "/education/homework and study tips"\n    }\n  ]\n}', '{\n  "usage": {\n    "text_units": 1,\n  
  "text_characters": 80,\n    "features": 1\n  },\n  "language": "en",\n  "categories": [\n    {\n      "score": 0.822388,\n      "label": "/automotive and vehicles/cars/sedan"\n    },\n    {\n      "score": 0.754857,\n      "label": "/automotive and vehicles/cars/hybrid"\n    },\n    {\n      "score": 0.68194,\n      "label": "/automotive and vehicles/cars/car culture"\n    }\n  ]\n}', '{\n  "usage": {\n    "text_units": 1,\n    "text_characters": 13,\n    "features": 1\n  },\n  "language": "en",\n  "categories": [\n    {\n      "score": 0.921873,\n      "label": "/real estate/architects"\n    },\n    {\n      "score": 0.790383,\n      "label": "/business and industrial/construction"\n    }\n  ]\n}', '{\n  "usage": {\n    "text_units": 1,\n    "text_characters": 52,\n    "features": 1\n  },\n  "language": "en",\n  "categories": [\n    
{\n      "score": 0.939684,\n      "label": "/technology and computing/computer reviews"\n    },\n    {\n      "score": 0.931032,\n      "label": "/technology and computing/tech news"\n    }\n  ]\n}', '{\n  "usage": {\n    "text_units": 1,\n    "text_characters": 14,\n    "features": 1\n  },\n  "language": "en",\n  "categories": [\n    {\n      "score": 0.979729,\n      "label": "/education/homework and study tips"\n    
},\n    {\n      "score": 0.850809,\n      "label": "/real estate/architects"\n    }\n  ]\n}', '{\n  "usage": {\n    "text_units": 1,\n    "text_characters": 28,\n    "features": 1\n  },\n  "language": "en",\n  "categories": [\n    {\n      "score": 0.999484,\n      "label": "/education/teaching and classroom resources"\n    }\n  ]\n}', '{\n  "usage": {\n    "text_units": 1,\n    "text_characters": 113,\n    "features": 
1\n  },\n  "language": "en",\n  "categories": [\n    {\n      "score": 0.734718,\n      "label": "/business and industrial/advertising and marketing/marketing"\n    },\n    {\n      "score": 0.675452,\n      "label": "/business and industrial/business operations/management/business process"\n    },\n    {\n      "score": 0.620496,\n      "label": "/business and industrial/advertising and marketing/brand management"\n    
}\n  ]\n}', '{\n  "usage": {\n    "text_units": 1,\n    "text_characters": 215,\n    "features": 1\n  },\n  "language": "en",\n  "categories": [\n    {\n      "score": 0.913644,\n      "label": "/technology and computing/operating systems"\n    },\n    {\n      "score": 0.855434,\n      "label": "/technology and computing/hardware/computer"\n    },\n    {\n      "score": 0.842841,\n      "label": "/technology and computing/hardware/computer components/chips and processors"\n    }\n  ]\n}']

【问题讨论】:

    标签: python arrays json


    【解决方案1】:

    根本问题是您要创建多个 JSON 字符串 (json_data = (json.dumps(response, indent=2))),然后将它们添加到数组 (json_arr.append(json_data)),然后将该数组转换为 JSON (json.dump(json_arr,outline))。所以结果是 strings 的 JSON 数组(包含 JSON)。

    您不想在添加到数组之前创建字符串,只需将response 包含在json_arr 中即可。然后通过您现有的json.dump(json_arr,outline) 调用将它们全部转换在一起:

    json_arr = []
    for text in gtm_Q6_df['ANSWER']:
        response = natural_language_understanding.analyze(
            text=text,language = 'en',
                features=Features(
                categories=CategoriesOptions(limit=3),
                )).get_result()
        json_arr.append (response) # <==== change is here
        with open('data.json','w') as outline: #with open('data.json','w') as outline:
            json.dump(json_arr,outline)
        
    print("break")
    print(json_arr)
    

    您可能需要稍微调整一下,我不会做太多 Python,但基本问题是您将每个 reponse 中的数据转换为 JSON 两次。只做一次,最后一起做。

    【讨论】:

    • 非常感谢,这个解决方案是正确的。
    猜你喜欢
    • 2023-03-24
    • 2023-03-19
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-28
    • 2013-06-14
    • 2015-10-03
    相关资源
    最近更新 更多