首页 > 解决方案 > 找到一个字符串并提取字符串后面的数字

问题描述

我有一个字符串如下

{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 新手。谢谢!

标签: pythonstring

解决方案


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 == "."])

推荐阅读