1

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?

4
  • You should probably escape the .s in your regular expressions as \. since you only want to match literal periods not any characters. Commented Sep 1, 2017 at 20:12
  • You could also use this regex: .*?(?:(?: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 Commented Sep 1, 2017 at 20:18
  • @ctwheels That's helpful, but would there be a way to do this as a dictionary with multiple values per key instead? I need it in this format: {'txup': ['hh:mm:ss', 'hh:mm:ss'], 'txdown': ['hh:mm:ss', 'hh:mm:ss']} Commented Sep 1, 2017 at 20:29
  • @WynneT I'm not exactly sure with python; it's been a while. You could, however, always remap the values based on key. Commented Sep 1, 2017 at 20:37

1 Answer 1

1

You were super close! You just needed to change group(0) to group(1) to get the right capture group. Fixed up:

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})"]) 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'] for entry in entries: for i in range(len(dtlist)): local_time = datetime.strptime(re.search(dtlist[i][1], entry).group(1), '%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_times) d['uttime'] = uttime 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.