python-3.x - 在python3中操作类型字节
问题描述
你能帮我从这个字节中获取报告的ID吗?
b'{
"message":"The report with id 9520 was created but it is invalid!",
"violations":"{\\"schemaLocation\\":\\"#\\",\\"pointerToViolation\\":\\"#\\",\\"causingExceptions\\":[{\\"schemaLocation\\":\\"#\\",\\"pointerToViolation\\":\\"#\\",\\"causingExceptions\\":[],\\"keyword\\":\\"required\\",\\"message\\":\\"required key [reportType] not found\\"},{\\"schemaLocation\\":\\"#\\",\\"pointerToViolation\\":\\"#\\",\\"causingExceptions\\":[],\\"keyword\\":\\"required\\",\\"message\\":\\"required key [finishedAt] not found\\"},{\\"schemaLocation\\":\\"#/properties/hosts/items\\",\\"pointerToViolation\\":\\"#/hosts/0\\",\\"causingExceptions\\":[],\\"keyword\\":\\"required\\",\\"message\\":\\"required key [applications] not found\\"}],\\"message\\":\\"3 schema violations found\\"}",
"valid":false
}'
解决方案
您还没有清楚地提到所有可能的输入。但是,对于给定的输入,下面的程序将起作用!这有点硬编码!
import json
data=b'{"message":"The report with id 9520 was created but it is invalid!", "violations":"{\\"schemaLocation\\":\\"#\\",\\"pointerToViolation\\":\\"#\\",\\"causingExceptions\\":[{\\"schemaLocation\\":\\"#\\",\\"pointerToViolation\\":\\"#\\",\\"causingExceptions\\":[],\\"keyword\\":\\"required\\",\\"message\\":\\"required key [reportType] not found\\"},{\\"schemaLocation\\":\\"#\\",\\"pointerToViolation\\":\\"#\\",\\"causingExceptions\\":[],\\"keyword\\":\\"required\\",\\"message\\":\\"required key [finishedAt] not found\\"},{\\"schemaLocation\\":\\"#/properties/hosts/items\\",\\"pointerToViolation\\":\\"#/hosts/0\\",\\"causingExceptions\\":[],\\"keyword\\":\\"required\\",\\"message\\":\\"required key [applications] not found\\"}],\\"message\\":\\"3 schema violations found\\"}","valid":false}'
data = json.loads(data)["message"].split()
data
是一个包含message
键值的列表!您可以对其进行索引以获得所需的值!
>>> data
['The', 'report', 'with', 'id', '9520', 'was', 'created', 'but', 'it', 'is', 'invalid!']
推荐阅读
- material-ui - 使用 react-select Select 在同一个网格中时,文本字段不会拉伸
- c# - 如何在容器中的应用程序入口点设置自动暂停以进行远程调试?
- docker - 有什么方法可以关闭 kubelet 中的驱逐?
- rust - 如何设计一个具有在编译时决定的可选特性的 Rust 结构?
- css - 如何将边框置于悬停中心
- android - 如何修复使用 Unity 3D Android X509TrustManager 制作的游戏被 Google Play 拒绝?
- asp.net - 带有 iFrame 的 ASP.Net 页面 - X-Frame-Options
- javascript - iframes - 在不指定域名的情况下更改父母的 URL
- python - 反转交叉表或频率表
- javascript - 使用 connectedRouter 时 Redux 中出现意外的键、“长度”、“动作”、“位置”等