简体   繁体   English

Python将格式化的字符串解析为{Timestamp}

[英]Python parse a formatted string to {Timestamp}

I'm loading a string value ({str}) from a data base this is the value: 我正在从数据库中加载一个字符串值({str}),该值是:

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

Now i need to convert it to {Timestamp} format. 现在,我需要将其转换为{Timestamp}格式。

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. 我知道它已经采用了正确的格式,但是我是Python新手,我想我缺少了一些东西。

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. 要使用datetime.strptime我需要一种格式,因为该字符串已经格式化,所以我希望在不显式构建格式的情况下进行解析。

Use datetime.strptime(string, format) method. 使用datetime.strptime(string,format)方法。 Find the difference between the two methods: strptime = "string parse time" strftime = "string format time" 找到两种方法之间的区别:strptime =“字符串解析时间” strftime =“字符串格式时间”

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. 但是,标准库无法处理数据的全部时间精度-它接受6位数的微秒字段,而不是7位数。 And it doesn't handle single letter timezone codes, you'll need to use a 3rd-party module for that. 而且它不处理单个字母的时区代码,因此您需要使用第3方模块。 However, if all of your strings use the 'Z' timezone, that's pretty easy to deal with since that's the UTC zone, ie, it has zero offset from UTC. 但是,如果您所有的字符串都使用“ Z”时区,那么这很容易处理,因为它是UTC时区,即与UTC的偏移量为零。

Here's some code that gets close to what you want using datetime.strptime to do the date / time parsing. 这是一些使用datetime.strptime进行日期/时间解析所需的代码。 It simply ignores the last two chars of the time data and replaces the 'Z' with the 'UTC' timezone string. 它只是忽略了时间数据的最后两个字符,并将“ Z”替换为“ UTC”时区字符串。

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)

The best solution is using parser: 最好的解决方案是使用解析器:

from dateutil import parser
date_obj = parser.parse(date_string)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM