python - 直接从子进程输出解析 json 输出
问题描述
我无法弄清楚如何直接从子进程输出解析 json 输出。
代码片段
cmd = ['./mmdbinspect', '--db', '/usr/local/var/GeoIP/GeoLite2-City.mmdb', ip]
# result returns json
result = subprocess.run(cmd, stdout=subprocess.PIPE)
result = json.loads(result)
# parse & print
print(result[0]['Lookup'])
print(result[0]['Records'][0]['Record']['country']['iso_code'])
print(result[0]['Records'][0]['Record']['country']['names']['en'])
如果我将结果写入文件,然后执行 json.load 它按预期工作,但我想合并并跳过该步骤。
回溯错误
TypeError: the JSON object must be str, bytes or bytearray, not CompletedProcess
解决方案
从文档返回的实例将具有属性 args、returncode、stdout 和 stderr。要使用它,请从标准输出加载 json 字符串。
result = json.loads(result.stdout)
推荐阅读
- c# - SSRS 2016:将报表加载到服务器时出现 NullReferenceException 和 ReportServerException
- python-3.x - 如何优化此 python 代码以对大输入进行排序?
- ms-access - MS-Access 表单中的动态查询
- three.js - Aframe:多网格模型的 glTF 大小调整和重新居中
- java - 如何以编程方式阻止应用程序中的 chromecast / MHL 屏幕镜像?
- excel - VBA Worksheet_Change 如何隐藏其中具有特定值的行
- google-cloud-firestore - 如何配置 GatsbyJS firestore 插件。timestampsInSnapshots 更改
- python - Python:为什么 unittest assertEqual 在这些列表上抛出错误而不是失败?
- python - 条件选择是否保留 Pandas DataFrame 中的顺序?
- javascript - 如何防止 keyup.enter 和登录按钮 onclick 之间的重复函数调用?