0

I need to convert date string "Dec 17 00:00:06" to string "2014-12-17 00:00:06" in python. I looked at the datetime.strptime but still can't find a way for this. eg:

Dec 17 00:00:06 to 2014-12-17 00:00:06

s_m
  • 83
  • 1
  • 1
  • 9

3 Answers3

2

You can use datetime module.

For e.g.

>>> import datetime
>>> do = datetime.datetime.strptime("Dec 17 2014 00:00:06", "%b %d %Y %H:%M:%S")
>>> do.strftime("%Y-%m-%d %H:%M:%S")
'2014-12-17 00:00:06'

We can replace year value to 2014 like following:

>>> do = datetime.datetime.strptime("Dec 17 00:00:06", "%b %d %H:%M:%S")
>>> do.strftime("%Y-%m-%d %H:%M:%S")
'1900-12-17 00:00:06'
>>> do1 = do.replace(year=2014)
>>> do1.strftime("%Y-%m-%d %H:%M:%S")
'2014-12-17 00:00:06'

If we want only time value then we can try like-

>>> do = datetime.datetime.strptime("Dec 17 00:00:06", "%b %d %H:%M:%S")
>>> do.strftime("%H:%M:%S")
'00:00:06'

Updated: update datetime string from file with its time string.

Algo:

  1. Get content from the input text file.
  2. Used re module i.e regular expression to get all pattern from content.
  3. Use set method to remove duplicate values.
  4. Convert datetime string to time string and update content.
  5. Write new content into same file or other file.

code is:-

import datetime
import re
p = "/home/vivek/Desktop/input.txt"
with open(p, "rb") as fp:
    content = fp.read()

date_values = set(re.findall("\[([^]]+)\]", content))
for i in date_values:
    do = datetime.datetime.strptime(i, "%b %d %H:%M:%S")
    content = content.replace("[%s]"%i, "%s"%(do.strftime("%H:%M:%S")) )

p = "/home/vivek/Desktop/output.txt"
with open(p, "wb") as fp:
    fp.write(content)
Vivek Sable
  • 9,938
  • 3
  • 40
  • 56
  • there is no year over there. – s_m Jan 12 '15 at 11:20
  • is there any possibility to remove all year and month from there. i need o/p as : Dec 17 00:00:06 to 00:00:06 – s_m Jan 12 '15 at 11:23
  • @SnehasishMohanta: Update according to your request, Can you check? – Vivek Sable Jan 12 '15 at 11:28
  • ok, every line in the text file contains such date formats `Dec 17 00:00:06`. right? So we have to create new file with time format`00:00:06`. right? – Vivek Sable Jan 12 '15 at 11:36
  • ok, `Dec 17 00:00:05` this format is always inside square bracket `[]`. right? – Vivek Sable Jan 12 '15 at 11:38
  • yes i got. but i am having a text file name " a.txt" which contain a same format like below: [Dec 17 00:00:00] stives, 0, [Dec 17 00:00:04] westjackets, 0, [Dec 17 00:00:05] combo, 10, [Dec 17 00:00:05] combo, 10, [Dec 17 00:00:05] comb, 10, [Dec 17 00:00:06] combo, 10, [Dec 17 00:00:06] combo, 10, [Dec 17 00:00:06] combo, 10, from this i need : 00:00:00 , stives, 0, 00:00:04 , westjackets, 0, 00:00:05 , combo, 10, 00:00:05 , combo, 10, 00:00:05 , comb, 10, 00:00:06 , combo, 10, 00:00:06 , combo, 10, 00:00:06 , combo, 10, is it possible – s_m Jan 12 '15 at 11:44
  • ok, We have to replace ` [Dec 17 00:00:05]` with `[00:00:05]` in same file or need to write new file with these chnages? – Vivek Sable Jan 12 '15 at 11:47
  • yes this format always in square bracket.but i need to remove it – s_m Jan 12 '15 at 11:47
  • it's ok if it can be replace then good. other wise we can make a new file – s_m Jan 12 '15 at 11:48
  • square bracket i dont want – s_m Jan 12 '15 at 11:48
  • @SnehasishMohanta: Can you check again, I updated my solution? – Vivek Sable Jan 12 '15 at 12:01
  • @SnehasishMohanta: update update with file opretion work for you or get any more exception in code? if any mail me vivekbsable@gmail.com – Vivek Sable Jan 12 '15 at 12:12
  • what could be the pattern of " 2014-12-17 00:00:15.85 " – s_m Jan 13 '15 at 05:51
  • means you want to give space " " at start and end of date string ? – Vivek Sable Jan 13 '15 at 06:10
  • no. i just want to know the pattern of 2014-12-17 00:00:15.85 – s_m Jan 13 '15 at 06:16
  • by using `strftime` method of `datetime` module qw can generate any type of datetime format e.g do1.strftime("%Y-%m-%d %H:%M:%S") and also check email. – Vivek Sable Jan 13 '15 at 06:41
0

Using datetime module will help you out.

from datetime import datetime

Firstly convert your date string to a datetime object by writing the line below.

date_obj = datetime.strptime("Dec 17 2014 00:00:06", "%b %d %Y %H:%M:%S")

Then convert that date object to a string.

date_obj.strftime("%Y-%m-%d %H:%M:%S")

You can change the parameters of the above strftime() function, and get different formats as per your requirments. Please follow python datetime

Here is the link to the options for the parameters which can be passed in the strftime() function parameter options

Go and play around as per your requirement.

Prateek
  • 1,538
  • 13
  • 22
0
from dateutil import parser
parser.parse('Dec 17 00:00:06').isoformat()
>>> '2015-12-17T00:00:06'
John Greenall
  • 1,670
  • 11
  • 17