python - 解析元组时出现ValueError?
问题描述
我的剧本是——
import subprocess
z = subprocess.check_output(['python3', 'test1.py'],universal_newlines=True)
mas = z
mas = dict(mas)
e = mas.get('email')
print(e)
我得到一个错误-
ValueError: dictionary update sequence element #0 has length 1; 2 is required
但是当我跑——
mas = {'email': 'something@gmail.com'}
mas = dict(mas)
e = mas.get('email')
print(e)
我得到了所需的输出,即something@gmail.com
. 此外,的输出test1.py
是{'email': 'something@gmail.com'}
解决方案
check_output()
将进程输出作为字符串返回。如果你知道它代表一个 Python 字典,你可以使用ast.literal_eval()
:
import ast
import subprocess
z = subprocess.check_output(['python3', 'test1.py'], universal_newlines=True)
mas = ast.literal_eval(z)
也就是说,我宁愿将 python 文件作为模块导入并直接使用其中的函数。
推荐阅读
- javascript - 实时求和表单列
- linux-device-driver - 全新安装在 HP Elitebook 840 G7 中后,触摸板在 Cent OS 7.9 中完全不工作
- delphi - 用于将 TClientDataSet 数据分组和聚合到另一个数据集中的内置解决方案
- python - 打印警告 - sqlalchemy
- python - 未显示西班牙地区的 Plotly Choropleth 地图
- analytics - 如何正确将 OneSignal 设置为 Segment (segment.com) 中的目的地,以及如何通过 Segment 设置 OneSignal 的外部用户 ID
- azure - 获取 Databricks Hive 元存储连接用户名和密码
- next.js - 无头 UI - 弹出框 - 菜单项状态
- magnolia - 国际化版本选择器输入提示
- java - NetBeans 12.3 中 JAX-RS RESTful 中的 RuntimeException