I am trying to increase all the timestamps (forms "HH: MM: SS") in a text file by a few seconds specified by the command line parameter for my program.
Here is a simplified version of my efforts:
import re from datetime import datetime, timedelta time_diff = timedelta(seconds=10) def replace_time(matchobj): if matchobj.group(1) not in [None, '']: return (datetime.strptime(matchobj.group(1), "%H:%M:%S") + time_diff).strftime("%H:%M:%S") print re.sub('(\d\d:\d\d:\d\d)', replace_time, "01:27:55")
This works great: the result of doing this 01:28:05 is what I want.
However, I heard that I should use global variables as little as possible. So I was wondering if there is an easy way to pass time_diff as a replace_time argument instead of using a global variable.
I tried the obvious, but this failed:
def replace_time(matchobj, time_diff): if matchobj.group(1) not in [None, '']: return (datetime.strptime(matchobj.group(1), "%H:%M:%S") + time_diff).strftime("%H:%M:%S") time_diff = timedelta(seconds=10) print re.sub('(\d\d:\d\d:\d\d)', replace_time(matchobj, time_diff), "01:27:55")
with this error: NameError: name 'matchobj' is not defined , so I cannot pass matchobj directly.
I reviewed the standard re page and the standard re howto , but cannot find the information I need. How can I avoid using a global variable here? Can I somehow pass an additional argument to the replace_time function? Thanks in advance.