【发布时间】: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}']
【问题讨论】: