python - Python:仅从字符串中提取 00-00-00 格式的数字
问题描述
我有一个这样的字符串:Test Step 01-08-04 U upper 100 m/s
目前我使用:
self.current_xtp_test_case = re.findall("\d+", test_case.attrib["name"])
self.current_xtp_test_case = ''.join(map(str,self.current_xtp_test_case[-1:]))
self.current_xtp_test_case = self.current_xtp_test_case.lstrip("0")
上面应该得到 00-00-00 模式中的最后一个数字,最终应该是4
因为我需要删除0
. 但是现在有了我正在处理的数据,有时可能会有一个数字,而不是00-00-00
上面的例子,我在最后的字符串中有 100,这弄乱了我当前代码的逻辑。
如何更改我的代码,以便我只能选择模式 01-03-04 中的最后一个数字 - 我只想选择04
零件。希望这是有道理的?
解决方案
你可以这样做:
s = ['Test Step 00-00-00 U upper 0 m/s',
'Test Step 00-00-09 U upper 100 m/s',
'Test Step 01-08-04 U upper 150 m/s']
[re.findall('[0-9]{2,}\-[0-9]{2,}\-([0-9]{2,})',x)[0] for x in s]
输出:
['00', '09', '04']
希望有所帮助;)
推荐阅读
- javascript - React Native Expo-Camera 录制视频发现问题用户拒绝需要音频权限
- python - facebook广告api的python字符串字典
- python - 给定 A = S*D*ST (D 是对角矩阵,S/ST 是任意 nxn 矩阵) A 的特征值不应该对应于 D 的对角线条目吗?
- c# - 我怎样才能让我的角色立足于unity3d
- python - 我似乎无法从网络上抓取 href 或文本
- permissions - Microsoft graph api:有些用户在使用我的应用程序时得到 403,而其他用户却没有,为什么?
- reactjs - 如何在调度挂钩 Reactjs 中发送回调
- swiftui - SwiftUI 自定义选项卡栏导航:图标选项卡时返回根视图
- excel - 将带有标签的多行文本也从 Word 插入到一个 Excel 单元格中
- php - Laravel Eloquent withCount() 不应用额外的约束