python - 使用 xmltodict 解析器对 json api 进行编码
问题描述
api 工作正常,但是当我尝试用包含特殊字符(如“pé”)的 char 值填充 package_description 字段时,会出现验证错误,我正在使用 xmltodict 库从 xml 文件中解析
这是我的代码
PS我正在使用python2.7
payload = "".join(
[
(self.name),
self.tag_end("code"),
(self.description_package or ""),
self.tag_end("description"),
self.new_line(),
self.get_tab(),
self.sub_tag_start("dateModification"),
self.tag_end("motif"),
self.new_line(),
self.get_tab(),
self.tag_end("package"),
]
)
response = requests.request("POST", url, headers=headers, data=payload)
res = json.loads(
json.dumps(xmltodict.parse(response.text, encoding='utf-8', process_namespaces=True))
)
if response.status_code != 200:
message = ""
description = ""
guid = ""
res = json.loads(
json.dumps(xmltodict.parse(response.text, encoding='utf-8', process_namespaces=True))
)
if (
res
and res.get("http://www.web.com/something:reponse")
and res.get("http://www.web.com/something:reponse").get(
"http://www.web.com/something:description"
)
):
message = res["http://www.web.com/something:reponse"][
"http://www.web.com/something:message"
]
description = res["http://www.web.com/something:reponse"][
"http://www.web.com/something:description"
]
guid = res["http://www.web.com/something:reponse"][
"http://www.web.com/something:guid"
]
_logger.warning("\nERROR MESSAGE: \n\n %s \n\n" % str(response.text))
raise ValidationError(
"%s \n\n %s \nGUID: %s" % (message, description, guid)
)
解决方案
推荐阅读
- kubernetes - http://localhost/graph 中没有自定义服务指标
- javascript - Next Js 静态资产在部署到 Plesk 时出现 500 内部服务器错误
- material-ui - Material-UI 主题在共享组件中不起作用
- deep-learning - 用文本注释图像以进行深度学习 + NLP
- python - pylatex.errors.CompilerError: 使用 Crontab 执行时未找到 LaTex 编译器
- javascript - 使用 Fetch 方法将属性提取到 HTML
- node.js - MongoDB:在回调中循环保存集合?
- terminal - 如何从 Monolog 查看实时记录数据?
- css - TailwindCSS 嵌套不适用于 tailwindcss/nesting 或 postcss-nesting
- mongodb - 使用 mongoexport 导出所有数据库