python - 找到一个字符串并提取字符串后面的数字
问题描述
我有一个字符串如下
{u'contentDescription': u'0 Minutes 3 SecondsElapsed3 Minutes 35 SecondsDuration', u'checked': False, u'clickable': False, u'scrollable': False, u'text': None, u'selected': False, u'enabled': True, u'bounds': {u'top': 1320, u'left': 48, u'right': 1032, u'bottom': 1386}, u'focusable': True, u'focused': False, u'checkable': False, u'longClickable': False, u'visibleBounds': {u'top': 1320, u'left': 48, u'right': 1032, u'bottom': 1386}, u'childCount': 0}
如何在 python 中解析这个字符串,以便我在 Duration 之前和 Elapsed 之后获得数字。即,3.35。
它出现在字符串的第一行。3 分 35 秒。我只想以 3.35 格式提取这些数字。
你能指导我如何做到这一点吗?我是 python 新手。谢谢!
解决方案
my_str
是整个字符串,或者如果将其转换为字典,则等于my_converted_dict['contentDescription']
:
''.join([val for val in my_str[my_str.find("Elapsed") + len("Elapsed"):my_str.find("Duration")].replace("Minutes", ".") if val.isdigit() or val == "."])
推荐阅读
- javascript - 为什么对复制的数组进行排序会对原始数组进行排序
- prolog - 如何在 Web 应用程序中集成 Prolog?
- date - 类似的数组公式工作,非数组公式工作,为什么不是这个数组公式?
- c++ - unordered_set 通过 lambdas 自定义哈希
- javascript - 如何在父 App.js 中获取导航栏子组件状态?
- php - 如何将 \x 格式的 UTF-8 字符串解码为 Unicode?
- java - 如何安排功能以间隔执行
- mongodb - 在 Mac 上运行 MongoDB Compass
- typescript - 在 typescript 3.8 promise 中使用“this”是未定义的
- r - 如何重新格式化数据框以计算 R 中的基本值(平均值、SD、SE 等)?