0

I have got a string of time data: "2019-08-02 18:18:06.02887" and I am trying to rewrite it as another string "EV190802_181802" in another file.

What I am trying now is splitting the string into lists and reconstructing another string by those lists:

hello=data.split(' ')
date=hello[0]
time=hello[1]
world=hello[0].split('-')
stack=time.split('.')
overflow=stack[0].split(':')
print('EV' + world[0] + world[1] + world[2] + '_' + overflow[0] + overflow[1] + overflow[2])

However, I have no idea how to remove 20 in 2019/world[0]. Is there any way I could remove '20'?

If there are alternative methods to rewrite the string, welcome to suggest as well.

ekhumoro
  • 98,079
  • 17
  • 183
  • 279
Simon219
  • 29
  • 6
  • 1
    Just to be sure: Is it "EV190802_181802" or "EV190802_181806" (with "06" at the end?) – akensert Aug 08 '20 at 10:49
  • That was a typo. Sorry for that. – Simon219 Aug 08 '20 at 16:46
  • Sorry for duplication of the question. Anyway, thanks for the answers, guys. I am not sure should I delete the question as some people here post answers different from another one's: https://stackoverflow.com/questions/2265357/parse-date-string-and-change-format. – Simon219 Aug 08 '20 at 17:07

5 Answers5

2

Just another way to solve the problem,

>>> from datetime import datetime
>>> 
>>> format_ = datetime.strptime("2019-08-02 18:18:06.02887", 
...                             "%Y-%m-%d %H:%M:%S.%f")
>>> 
>>> print(
    format_.strftime('EV%y%m%d_%H%M') + format_.strftime('%f')[:2]
)

EV190802_181802
sushanth
  • 6,960
  • 3
  • 13
  • 23
  • 1
    Why not simply `dt.strftime('EV%y%m%d_%H%M%S')` => `'EV190802_181806'`? – ekhumoro Aug 08 '20 at 11:10
  • Okay - but also note that the OP doesn't want `EV190802_181802` as the output. You can see this by running the code they wrote, which prints `EV20190802_181806`. (The question has a typo at the beginning). – ekhumoro Aug 08 '20 at 11:32
0

Using regex:

import re

data = "2019-08-02 18:18:06.02887"
res = re.match(r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})\s(?P<hours>\d{2}):(?P<minutes>\d{2}):(?P<seconds>\d{2}).(?P<miliseconds>\d+)',data)

out = f"EV{res.group('year')[2:]}{res.group('month')}{res.group('day')}_{res.group('hours')}{res.group('minutes')}{res.group('miliseconds')[:2]}"
print(out) 

Output will be:

EV190802_181802
bp7070
  • 172
  • 1
  • 6
0

I would use re.sub here for a regex approach:

inp = "2019-08-02 18:18:06.02887"
output = re.sub(r'^\d{2}(\d{2})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2}).*$',
                'EV\\1\\2\\3_\\4\\5\\6',
                inp)
print(output)

This prints:

EV190802_181806

Note: Your expected output was actually given as EV190802_181802, but it appears to be a typo for my solution above, as I see no reason why you would not want to report seconds, but instead report hundreth fractions of a second.

Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
0
  1. Remove all occurrences of -, . and :

     hello.replace("-","")
     hello.replace(".","")
     hello.replace(":","")
    
  2. Get string in one line:

     print("EV" + hello[2:8] + "_" + hello[9:15])
    
Adam Elm
  • 46
  • 6
0

Without regex, just string splits and joins:

string = "2019-08-02 18:18:06.02887"
target = "EV190802_181806" 

d, t = string.split("20")[-1].split(".")[0].split(" ")
print("date:", d, "\ntime:", t)

d = "".join(d.split('-'))
t = "".join(t.split(':'))
result = "EV" + d + "_" + t
print("\nresult: ", result)

assert result == target

# >> out:
# date: 19-08-02 
# time: 18:18:06
# 
# result:  EV190802_181806

I make the assumption that you'd like to have "06" at the end of the target string (sorry if I'm mistaken!).

akensert
  • 224
  • 1
  • 2
  • 6