python - 使用包含 html、转义字符和换行符的文件解析 Json
问题描述
我正在尝试将下面的 JSON 提取到 csv 文件中,由于转义字符,它无法解析字段“questionLBL”,\n,。我也尝试过替换,但没有太大帮助。
谁能帮我解析这个JSON?
{
"question": {
"active": true,
"itemId": "746",
"alternative": null,
"searchTerm": null,
"answer": "50000",
"enumerationCodeDelimiters": null,
"dataType": "String",
"multiValued": false,
"smId": "0778299",
"workType": "REQUEST",
"type": "External",
"totalQuestions": "23",
"Label": "Request Form",
"questionLBL": "Request Type\n<em style=\"color: rgb(255, 0, 0); font-family: Arial, Verdana, sans-serif; font-size: 13px;\">If you are unsure which request type to use, please see the attached Reference file.</em>"
}}
我尝试了许多其他方式的示例之一:
import json
import csv
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
with open("test_2020.json", "r") as rf:
decoded_data = json.load(rf)
#print(decoded_data)
# Check is the json object was loaded correctly
try:
with open("test.csv","w") as outfile:
f = csv.writer(outfile,delimiter='|',quoting=csv.QUOTE_ALL)
for i in decoded_data:
f.writerow([i.get(("active"),"null"),i.get(("itemId"),"null"),i.get(("alternative"),"null")\
i.get(("searchTerm"),"null") ,i.get(("answer"),"null"),\
i.get(("enumerationCodeDelimiters"),"null"),i.get(("dataType"),"null"),i.get(("multiValued"),"null"),\
i.get(("smId"),"null"),i.get(("workType"),"null")\
i.get(("type"),"null"),\
i.get(("totalQuestions"),"null"),i.get(("Label"),"null"),i.get(("questionLBL"),"null"),\
])
except KeyError:
print("Error Found")
解决方案
推荐阅读
- android - 如何解决此问题“NoSuchFieldError: No static field INSTANCE of type...”?
- meshlab - 导入 Fusion 360 .stl 文件时,Sketchup 冻结
- amazon-web-services - AWS 第 7 层防火墙阻止 http 洪水
- javascript - GatsbyJS 中的 Markdown 文件在转换时未按预期转换为 HTML
- python - 如果关键字不失败,则重复执行关键字固定时间而不失败
- azure - 从 Azure Functions 连接到 tcp 套接字时出错
- c# - 警告 MSB3290:包装器组件失败
- python - SystemError:未加载父模块“setuptools”,无法执行相对导入
- python - charm.pool.map & tqdm:获取进度条
- java - 如何处理平面图中的异常 - 反应弹簧