0

I'm loading a string value ({str}) from a data base this is the value:

'W/"datetime\\'2017-10-16T20%3A18%3A02.2644265Z\\'"'

Now i need to convert it to {Timestamp} format.

Tried this:

from datetime import datetime
datetime.strftime(MyStrValue)

And got:

{TypeError}descriptor 'strftime' requires a 'datetime.date' object but received a 'str'

Tried this:

import dateutil.parser
dateutil.parser.parse(MyStrValue)

And got:

{ValueError}Unknown string format

I understand it's already in the right format but i'm Python newbie and i guess i'm missing something.

Edit:

to use datetime.strptime i need a format, since the string is already formatted i wish to parse it without explicitly building the format.

MTZ4
  • 2,274
  • 2
  • 25
  • 41
  • 3
    Possible duplicate of [Convert string date to timestamp in Python](https://stackoverflow.com/questions/9637838/convert-string-date-to-timestamp-in-python) – Shaido Dec 12 '17 at 06:44
  • You firstly need to extract only datetime str, and then with `.strptime()` convert to `datetime`. – vishes_shell Dec 12 '17 at 06:45
  • `datetime.strftime()` is not going to work because it takes a format and gives you the current datetime. You need a function that takes a string and gives you a datetime. As @vishes_shell pointed out, `datetime.strptime()` can do this, but you need to also enter a format. You'll have to figure the format your date string has. – Daniel F. Dec 12 '17 at 07:23

3 Answers3

0

Use datetime.strptime(string, format) method. Find the difference between the two methods: strptime = "string parse time" strftime = "string format time"

Sneha
  • 140
  • 6
  • I understand the difference, but the string is already formatted. I wish to parse it without reformatting it. Or alternatively use the existing format without explicitly building it. – MTZ4 Dec 12 '17 at 06:51
0

This is a little tricky. First, you need to extract the actual date / time string from your source string, then you need to convert the percent-encoded characters to proper chars, and finally you can parse the time and date from it.

However, the standard library can't handle the full time precision of your data - it accepts a 6 digit microseconds field, not 7 digits. And it doesn't handle single letter timezone codes, you'll need to use a 3rd-party module for that. However, if all of your strings use the 'Z' timezone, that's pretty easy to deal with since that's the UTC zone, i.e., it has zero offset from UTC.

Here's some code that gets close to what you want using datetime.strptime to do the date / time parsing. It simply ignores the last two chars of the time data and replaces the 'Z' with the 'UTC' timezone string.

BTW, I had to adjust your input string slightly: the string you posted in the question isn't a valid string literal.

from urllib.parse import unquote
from datetime import datetime

mystr = 'W/"datetime\'2017-10-16T20%3A18%3A02.2644265Z\'"'
print('Original:', repr(mystr))

# Split on single-quotes
fields = mystr.split("'")
print('Fields:', fields)

# Convert percent-encoded chars to proper chars
datestr = unquote(fields[1])
print('Date:', datestr)

# Trim the final digit and the time zone letter, replacing it with 'UTC'
datestr = datestr[:-2] + 'UTC'

#Convert to a datetime object
timestamp = datetime.strptime(datestr, '%Y-%m-%dT%H:%M:%S.%f%Z')
print('Timestamp:', timestamp, repr(timestamp))

output

Original: 'W/"datetime\'2017-10-16T20%3A18%3A02.2644265Z\'"'
Fields: ['W/"datetime', '2017-10-16T20%3A18%3A02.2644265Z', '"']
Date: 2017-10-16T20:18:02.2644265Z
Timestamp: 2017-10-16 20:18:02.264426 datetime.datetime(2017, 10, 16, 20, 18, 2, 264426)
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
-1

The best solution is using parser:

from dateutil import parser
date_obj = parser.parse(date_string)
RAJ GUPTA
  • 428
  • 5
  • 7
  • As i wrote i'm getting `{ValueError}Unknown string format` when using this expect code – MTZ4 Dec 12 '17 at 06:57