简体   繁体   English

将 Twitter 新日期格式转换为日期时间 ( %Y-%m-%d %H:%M%S' )

[英]Convert Twitter New Date Format to Date Time ( %Y-%m-%d %H:%M%S' )

Twitter's new date format used to be Twitter 的新日期格式曾经是

%a %b %d %H:%M%S    0000 %Y

Now however it is like this...然而现在是这样的……

df.created_at[0]
   '2021-03-08T19:14:00.000Z'

How do we convert that?我们如何转换它? What is confusing me is figuring out how to process the "T" and the ".000Z" portions.令我困惑的是弄清楚如何处理“T”和“.000Z”部分。

Thanks!谢谢!

E: E:

To be clear,https://developer.twitter.com/en/docs/twitter-ads-api/timezones says what the strptime is.需要明确的是,https://developer.twitter.com/en/docs/twitter-ads-api/timezones说明了 strptime 是什么。 However, it doesn't seem to work for me.但是,它似乎对我不起作用。

Trying my best guess...尝试我最好的猜测...

datetime.strftime( datetime.strptime( df.created_at[0], '%Y-%m-%dT%H:%M:%S.00%z' ), '%Y-%m-%d %H:%M:%S' )
ValueError: time data '2021-03-08T19:14:00.000Z' does not match format '%Y-%m-%dT%H:%M:%S.00%z'

Trying their exact match...尝试他们的精确匹配...

datetime.strftime( datetime.strptime( df.created_at[0], '%Y-%m-%dT%l:%M:%S%z' ), '%Y-%m-%d %H:%M:%S' )
ValueError: 'l' is a bad directive in format '%Y-%m-%dT%l:%M:%S%z'
>>> datetime.strftime( datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.00%z' ), '%Y-%m-%d %H:%M:%S' )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "/usr/lib/python3.8/_strptime.py", line 349, in _strptime
    raise ValueError("time data %r does not match format %r" %
ValueError: time data '2021-03-08T19:14:00.000Z' does not match format '%Y-%m-%dT%H:%M:%S.00%z'
>>> datetime.strftime( datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.000%z' ), '%Y-%m-%d %H:%M:%S' )
'2021-03-08 19:14:00'

You were just missing a 0 in your parsing string.您只是在解析字符串中缺少一个0

暂无
暂无

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

相关问题 熊猫:如何将日期格式%Y-%M-%D转换为%Y%M%D? - Pandas:How to convert date format %Y-%M-%D into %Y%M%D? ValueError:时间数据“ 140120 1520”与格式“%Y-%m-%d%H:%M:%S”不匹配 - ValueError: time data '140120 1520' does not match format '%Y-%m-%d %H:%M:%S' ValueError: 时间数据与格式 &#39;%Y-%m-%d %H:%M:%S.%f&#39; 不匹配 - ValueError: time data does not match format '%Y-%m-%d %H:%M:%S.%f' 出现错误“ValueError:时间数据&#39;&#39;与格式&#39;%Y-%m-%d %H:%M:%S&#39;不匹配” - Getting error "ValueError: time data '' does not match format '%Y-%m-%d %H:%M:%S'" ValueError: 时间数据“无”与格式“%Y-%m-%d %H:%M:%S”不匹配 - ValueError: time data 'None' does not match format '%Y-%m-%d %H:%M:%S' 时间数据与格式 '%Y-%m-%d %H:%M:%S' 不匹配 - time data does not match format '%Y-%m-%d %H:%M:%S' 如何将日期时间从十进制转换为“%y-%m-%d%H:%M:%S”给定时间原点? - How to convert datetime from decimal to “%y-%m-%d %H:%M:%S” given the time origin? Python 将日期类型转换为 %Y-%m-%d - Python convert date type to %Y-%m-%d 如何将 Python 日期格式 &#39;%B - %Y&#39; 转换回 &#39;%Y-%m-%d&#39;? - How to convert Python date format '%B - %Y' back to '%Y-%m-%d'? backtrader 时间列:ValueError:时间数据“0”与格式“%Y-%m-%d %H:%M:%S”不匹配 - backtrader time column : ValueError: time data '0' does not match format '%Y-%m-%d %H:%M:%S'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM