简体   繁体   English

在python中将字符串解析为时间值

[英]Parsing string to time value in python

I need to convert the following strings into either datetimes or strings in a different format. 我需要将以下字符串转换为日期时间或不同格式的字符串。 My input looks like these examples: 我的输入看起来像这些例子:

196h 26m 13s 196小时26分13秒
95h 19m 95h 19m
45m 28s 45米28秒

My desired string output would be (But my final goal is to convert these strings into datetime objects): 我想要的字符串输出是(但我的最终目标是将这些字符串转换为datetime对象):

196:26:13 196:26:13
95:19:00 95:19:00
00:45:28 ○时45分28秒

In Excel, it would be [h]:mm:ss . 在Excel中,它将是[h]:mm:ss

Note: As you can see, hours can be higher than 24. 注意:如您所见,小时数可能高于24小时。

I have tried to parse this string with the time.strptime() method, but it doesn't work with hours higher than 24. I have a solution with regular expressions, but I want to know if there is a more straightforward way to do this. 我试图用time.strptime()方法解析这个字符串,但它不适用于高于24的小时。我有一个正则表达式的解决方案,但我想知道是否有一种更直接的方法这个。 What is the best way to solve this? 解决这个问题的最佳方法是什么?

This would give you time deltas: 这会给你时间增量:

from datetime import timedelta

def to_timedelta(time_string):
    units = {'h': 'hours', 'd': 'days', 'm': 'minutes', 's': 'seconds'}
    return timedelta(**{units[x[-1]]: int(x[:-1]) for x in time_string.split()})

Test: 测试:

times = ['196h 26m 13s', '95h 19m', '45m 28s']
for t in times:
    print(to_timedelta(t))

Output: 输出:

8 days, 4:26:13
3 days, 23:19:00
0:45:28

timedelta takes these arguments; timedelta接受了这些论点;

datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0) datetime.timedelta(days = 0,seconds = 0,microseconds = 0,milliseconds = 0,minutes = 0,hours = 0,weeks = 0)

Using this mapping: 使用此映射:

units = {'h': 'hours', 'd': 'days', 'm': 'minutes', 's': 'seconds'}

allows the short units in the string to be mapped to the corresponding names of the arguments. 允许将字符串中的短单元映射到参数的相应名称。 Using Pythons ** syntax, the resulting dictionary can be used as single argument that will be converted in to the matching keyword arguments. 使用Pythons **语法,生成的字典可以用作单个参数,将转换为匹配的关键字参数。

The first thing we should do is use a regular expression and use timedelta instead of datetime. 我们应该做的第一件事是使用正则表达式并使用timedelta而不是datetime。

import datetime
import re

regex = re.compile(r'((?P<hours>\d+?)h)?((?P<minutes>\d+?)m)?((?P<seconds>\d+?)s)?')

def parse_time(time_str):
    parts = regex.match(time_str)
    if not parts:
        return
    parts = parts.groupdict()
    time_params = {}
    for (name, param) in parts.items():
        if param:
            time_params[name] = int(param)
    return datetime.timedelta(**time_params)


L = ["196h 26m 13s", "95h 19m", "45m 28s"]

for l in L:
    print(parse_time(l))

Output: 输出:

8 days, 4:00:00
3 days, 23:00:00
0:45:00

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

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