简体   繁体   English

如何在python中将有关时间的字符串拆分为单独的变量(hr,min,sec)?

[英]How can I split a string about time into separate variables (hr, min, sec) in python?

I'm using selenium to scrape a website with constantly updating alerts, and the duration section looks like '37s' or '20m 11s' or '1h 14m 45s'. 我正在使用硒抓取不断更新警报的网站,持续时间部分看起来像“ 37s”或“ 20m 11s”或“ 1h 14m 45s”。 The website updates 15 seconds or so, so the duration is always changing (unless the alert cleared, then it disappears completely.) 网站更新15秒左右,因此持续时间总是在变化(除非清除警报,然后警报完全消失)。

I'd like to create a way to be notified if an alert lasts longer than x amount of time, so I'd like to split up the time element and convert each piece into seconds. 如果警报持续的时间超过x的时间,我想创建一种通知方式,因此我想拆分时间元素并将每段转换为秒。

I've tried re.sub , re.split , and others to try and separate the sections. 我已经尝试过re.subre.split和其他尝试将各部分分开。 If it can be done within a single variable, that would be great. 如果可以在单个变量中完成操作,那就太好了。

# The Chrome frame you need to be in to access the web elements for the alerts.
browser.switch_to.frame('maincontentframe')          

# This variable contains the information contained in the 'duration' web element(s).  
durationElem = browser.find_elements_by_xpath("//table[@id='servicetable']//tr[starts-with(@class, 'servicerow')]//div[starts-with(@class, 'duration')]")         

# This loop allows you to see each alert's duration.
for elem in durationElem:  
    minutes, seconds = elem.text.split("m,s") 

I'd like to have some variables that contain just integers. 我想要一些仅包含整数的变量。 For example, if there's an alert that says '4m 10s', there would be two variables that contain 240 & 10. Or even just one variable containing 250 would work. 例如,如果有一个警报显示“ 4m 10s”,则将有两个包含240和10的变量。或者甚至只有一个包含250的变量都可以工作。

Well it appears the format you have provided has a space delimiter and can be separated at such. 好吧,您提供的格式似乎有一个空格定界符,可以这样分隔。

4hr 26min 30sec can be our example input 4hr 26min 30sec可以作为我们的示例输入

def findTotalSeconds(strInput):
    splitData = strInput.split(' ') # [4hr, 26min, 30sec]

    # Reverse the order (you will see why in a second)
    splitData.reverse() # [30sec, 26min, 4hr]

    # Isolate the integers
    parsedInts = []
    for parsedVal in splitData:
        parsedInts.append(int(''.join([s for s in parsedVal if s.isdigit()])))

    # parsedInts = [30, 26, 4]

    # Now you can mathematically convert the elements accordingly
    totalTime = 0
    for i in range(len(parsedInts)):
        totalTime += parsedInts[i] * (60 ** i)

    return totalTime

This logic for collecting the total time is as follows for our example 4hr 26min 30sec 对于我们的示例,收集总时间的逻辑如下所示: 4hr 26min 30sec

Generated splitData list: ['4hr', '26min', '30sec'] 生成的splitData列表: ['4hr', '26min', '30sec']

Reversed: ['30sec', '26min', '4hr'] 反转: ['30sec', '26min', '4hr']

Running through totalTime loop: (30 * (60 ** 0)) + (26 * (60 ** 1)) + (4 * (60 ** 2)) 通过totalTime循环运行: (30 * (60 ** 0)) + (26 * (60 ** 1)) + (4 * (60 ** 2))

Running through totalTime result: 30 + 1560 + 14400 = 15990 total seconds 运行totalTime结果: 30 + 1560 + 14400 = 15990 total seconds

def seconds(line):
    D = {word[-1]:int(word[:-1]) for word in line.split()}
    return D.get("h",0)*60*60+D.get("m",0)*60+D.get("s",0)

Assuming elem.text holds a text string like "1h 3m 17s" or "10m", this should do what you want 假设elem.text包含“ 1h 3m 17s”或“ 10m”之类的文本字符串,这应该做您想要的

import re
scale = {'h': 60*60, 'm': 60, 's': 1}
regex = re.compile("(\d+)([mhs])")

for elem in durationElem:
    seconds = 0
    for match in regex.finditer(elem.text):
        groups = match.groups()
        seconds += int(groups[0]) * scale[groups[1]]
    print(seconds)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM