How to extract and increment the hour from text (str) using Python

半城伤御伤魂 提交于 2019-12-02 23:21:02

问题


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.


回答1:


  1. Use pattern '[0-9:,]' to find all the time data.
  2. times.sort() can increase time in the list
  3. print 'it time %s' % str(times[0]) for instance, could print without changing the text


来源:https://stackoverflow.com/questions/25468319/how-to-extract-and-increment-the-hour-from-text-str-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!