gpt4 book ai didi

python 正则表达式子%H :%M:%S to %M:%s or 01:%M:%s

转载 作者:行者123 更新时间:2023-11-30 23:36:09 29 4
gpt4 key购买 nike

我想将字符串(字幕)转换为:

585
00:59:59,237 --> 01:00:01,105
- It's all right. - He saw us!

586
01:00:01,139 --> 01:00:03,408
I heard you the first time.

进入

59:59 - It's all right. - He saw us!

01:00:01 I heard you the first time.

*我想要的是:如果时间在一小时以内,则去掉“00:”前缀,而如果时间大于1小时,则保留它*

我的正则表达式是:

pat = re.compile(r"""
#\s* # Skip leading whitespace
\d+\s # remoe lines contain only numbers
((?:(?:00)|(?P<hour>01)):(?P<time>\d{2}:\d{2})[,0-9->]+.*)[\r\n]+(?P<content>.*)[\r\n]+
""",
re.VERBOSE)
data = pat.sub(r"\g<hour>\g<time> \g<content>", data)

仅当 ' \g<hour> 时才有效' 未使用。谁能帮助我吗?

最佳答案

我想,这就是您正在寻找的:

import re

s = """
585
00:59:59,237 --> 01:00:01,105
- It's all right. - He saw us!

586
01:00:01,139 --> 01:00:03,408
I heard you the first time.
"""

for line in re.findall(r'(\d+:)(\d+:\d+)(?:.*\n)(.*)', s):
if line[0] == '00:':
print ' '.join(line[1:])
else:
print ' '.join([''.join(line[0:2]), line[2]])

输出:

# 59:59 - It's all right. - He saw us!
# 01:00:01 I heard you the first time.

关于 python 正则表达式子%H :%M:%S to %M:%s or 01:%M:%s,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16759896/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com