Using regex in Python to insert a space between a date and time

Home / Uncategorized / Using regex in Python to insert a space between a date and time

Question:
I have a list of dates and times in the following format: 25/07/201711:00:00 I just want to insert whitespace between the date and time so it looks like: 25/07/2017 11:00:00

The string replace method works well but is not very robust i.e. mystring.replace("2017","2017 " ) works but only for 2017 dates. Regex sub method seems to be what I need to use but have not been successful so far. Any suggestions would be very helpful as my regex knowledge is limited.

This is closest from what I have tried:>>>re.sub(r’20[0-9][0-9]’, r’20[0-9][0-9] ‘, s)
’20[0-9][0-9] 04:00’


Answer:
If your dates are in ‘dd/mm/yyyy’ format, why can’t you just index your string?
>>> mystring[:10] + ‘ ‘ + mystring[10:]
’25/07/2017 11:00:00’
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *