python - 提取python字符串中给定键的值
问题描述
'2019-01-04T03:22:17.950795+00:00 CONSOLE:0 (null) - 01-04-2019 03:22:17.950 INFO (SGUI.APP) - report_event:{"event":"pip-started", "time":"110ms"}#012'
由此,我的目标是使用“事件”和“时间”作为识别价值的关键来提取“点子开始”和“110”
解决方案
此表达式可能会提取这些值:
import re
regex = r"\"event\":\"([^\"]+)\"|\"time\":\"(\d+)"
test_str = "2019-01-04T03:22:17.950795+00:00 CONSOLE:0 (null) - 01-04-2019 03:22:17.950 INFO (SGUI.APP) - report_event:{\"event\":\"pip-started\", \"time\":\"110ms\"}#012"
print(re.findall(regex, test_str))
输出
[('pip-started', ''), ('', '110')]
该表达式在regex101.com的右上角面板上进行了说明,如果您希望探索/简化/修改它,并且在此链接中,您可以查看它如何与一些示例输入匹配,如果您愿意的话。
推荐阅读
- python - 根据 pandas DataFrame 中的值序列生成索引元组
- uwp - UWP 应用程序的图标大小(通用 Windows 平台、AppX)
- python-3.x - 搜索图像中“最红”像素的坐标
- android - 使用观察到的数据更新 RecyclerView 的正确位置
- physics - 经典分子动力学
- javascript - 从子回调设置状态
- reactjs - Apollo 客户端异步刷新令牌
- cpu-cache - CPU缓存是如何工作的(里面问题的完整描述)?
- python - PyTorch - 你应该如何规范化单个实例
- python - 利用递归仅返回基本情况的优化问题(Python)