python - 检查字符串是否为大于 1 的数字
问题描述
我正在尝试检查字符串是否为大于 1 的数字。字符串格式为 Minutes:Seconds = 0:12
这是我的方法,但有时我会收到此错误“列表索引超出范围”
length = driver.find_element_by_xpath("/html/body/path[2]").text
# length output would be something like 0:28
x = list(length)
if int(x[0]) < 1:
print("Less than 1 minute.")
else:
pass
我怎样才能更有效地完成这项任务并避免出现“列表索引超出范围”错误
解决方案
如果length
总是:
介于分钟和秒之间,您可以使用字符串split
函数将其转换为列表。但是,如果您收到“列表索引超出范围错误”,您可能需要检查它length
是否具有您期望的格式。
x = length.split(":")
if int(x[0]) < 1:
print("Less than 1 minute.")
else:
pass
推荐阅读
- ios - TokBox webrtc 在 iOS 上失败
- javascript - 使用 React 和 Quill.js 加载时间很慢
- xml - Android Studio 和 Kotlin:ListView 不会使用 CoordinatorLayout 中的锚点固定在 app_bar 下方
- excel - 在工作簿 VBA 的第二个工作表上应用过滤器
- javascript - 使用 Set .forEach() 将项目添加到对象的指定属性
- laravel - Laravel 新命令不再适用于 MAC
- node.js - 使用cheerio获取选定元素的文本值
- python - 如何用熊猫填充一列,对一系列行执行操作?
- paypal - Odoo 12 CE 中的 Paypal Payment Acquirer 问题返回指向 loalhost 的 URL。如何将其更改为我的域?
- google-cloud-platform - 在 VPC 中使用本地 DNS 服务器