I'm parsing times from a text file which I've made into a list of strings called 'entries'. It looks like this (but with more entries):
['tx cycle.. up:01:22:53 tx cycle.. down:21:03:11', 'tx cycle.. up:12:43:31 tx cycle.. down:19:13:00', ...] I want to convert each time to a datetime format and add 4 hours to each of them, then store the new times in a dictionary with the appropriate keys 'txup' and 'txdown'.
Here's the code I have so far:
import re from datetime import datetime import pytz d = {} converted_times = [] local = pytz.timezone("Etc/GMT+4") dtlist = (["txup", "\s?tx\scycle.{0,4}\s?up:\s?(\d{1,2}:\d{2}:\d{2})"], ["txdown", "\s?tx\scycle.{0,4}\s?down:\s?(\d{1,2}:\d{2}:\d{2})"]) for entry in entries: for i in range(len(dtlist)): local_time = datetime.strptime(re.search(dtlist[i][1], entry).group(0), '%H:%M:%S') #first problem arises here localized_time = local.localize(local_time) utc_time = localized_time.astimezone(pytz.utc) converted_times.append(utc_time) uttime = map(str, converted_time) d['uttime'] = uttime The first problem is that I get the error 'time data 'tx cycle.. up:01:22:53' does not match format '%H:%M:%S''. I'm not sure why I'm getting this error since I have the appropriate capture group in my regex to capture just the time. Anyone know why this is happening?
Other than that, I'm not really sure as to how I can ensure that each time data is inserted under its correct key in my dictionary. Because as my code is right now, I'm pretty sure it would all be put under one key called 'uttime', but I actually want this divided up into the keys 'txup' and 'txdown'. Is there a simple way I can do this in my loop?
Edit: The regex problem is solved now after changing group(0) to group(1), but now when I'm trying to change the times according to the time zone adjustment I'm getting the error that 'datetime.time' object has no attribute 'astimezone'. I can't seem to find online what attribute I should be using instead - does anyone know?
.s in your regular expressions as\.since you only want to match literal periods not any characters..*?(?:(?:up:\s*(?<time_up>[\d:]+))|(?:down:\s*(?<time_down>[\d:]+)))and gather the named capture groups as your array instead of running two regular expressions. See this post for information on named capture groups in python