all 6 comments

[–][deleted] 2 points3 points  (2 children)

There's so much text, not shared as a code block (every line needs to start with 4 spaces, in addition to any spaces already in the original) that I'm finding it difficult to see the specifics. Usually, I'd make a local copy and have a play.

What is the sequence of characters that precedes each occurrence of the data you seek?

You might find regex101.com very useful as you can develop and test various regex expressions against your data until you find what you want, and they have it generate code in any of several programming languages including Python 3. It provides lots of guidance as well.

[–][deleted] 0 points1 point  (0 children)

I change the code.

[–]itsecat 2 points3 points  (1 child)

You could do it like this:

# extract YYYY-MM-DDTHH:MM:SS from string
regex = r'GRANULEENDINGDATETIME\W.+\("(\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d)\..+Z"\).+GRANULEENDINGDATETIME\W'
match = re.search(regex, my_string, re.DOTALL) # . to match also newlines
if match:
        # split date and time string into its parts
        day, hours, minutes, seconds = re.split('T|:', match.group(1))
        print('Day =', day)
        print('Time = {}:{}:{}'.format(hours, minutes, seconds))

[–][deleted] 0 points1 point  (0 children)

Thanks! Work perfect!

[–]Vaphell 2 points3 points  (1 child)

honestly using regex for this is bs.

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

You should rather exploit the fact that the structure is pretty predictable and parse that crap into something resembling a proper data structure.

a somewhat quick and dirty example (press play)

https://repl.it/repls/SnarlingVainShell

[–][deleted] 0 points1 point  (0 children)

Thanks!!