如何使用Python从文本(str)中提取和增加小时 [英] How to extract and increment the hour from text (str) using Python
本文介绍了如何使用Python从文本(str)中提取和增加小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,我有这段文字(输入):
For example I have this text (Input):
Event Time, Monday:
10:01:02,269 to 10:01:08,702
(Reported by John).
Event Time, Sunday:
20:01:08,931 to 20:01:15,234
(Reported by Peter).
...
然后您要更改并增加时间(时间),因此(输出):
Then you want to alter and increase the time (only the time), thus (Output):
Event Time, Monday:
10:01:02,369 to 10:01:09,002 #Change the microseconds and seconds
(Reported by John).
Event Time, Sunday:
20:01:09,131 to 20:01:15,934 #Change the microseconds and seconds
(Reported by Peter).
一旦有时间,我就可以改变。因此,问题在于如何只获得时间。我在想,也许一种方法是像这样收集列表中的时间:
Once I have the times, I can alter. So the problem is how to get only the times. I was thinking that maybe one way would be to collect the times in a list, like this:
times = ['10:01:02,269', '10:01:08,702', '20:01:08,931', '20:01:15,234']
但是如何做到这一点?然后,如何在列表中增加它?最后是如何在不更改文本的情况下进行打印。
But how to make that? and then, how to increase it in the list? And finally how to print without changing the text.
推荐答案
- 使用模式
'[0-9:,]'
查找所有时间数据。 -
times.sort()
可以增加列表中的时间 例如, -
打印'it time%s'%str(times [0])
,可以打印而无需更改文本
- Use pattern
'[0-9:,]'
to find all the time data. times.sort()
can increase time in the listprint 'it time %s' % str(times[0])
for instance, could print without changing the text
这篇关于如何使用Python从文本(str)中提取和增加小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文