[英]Error converting date from DD/MM/YYYY HH.mm.ss to MM-DD-YYYY HH:MM:SS in python
I have a dataset which has a column "timestamp" in the Italian format: 11/03/2004 00.00.00
and I need to convert it as 2004-03-11 00:00:00
.我有一个数据集,其中包含意大利语格式的“时间戳”列: 11/03/2004 00.00.00
,我需要将其转换为2004-03-11 00:00:00
。 I followed the guide on this old post but when I try to do this:我按照这个旧帖子上的指南进行操作,但是当我尝试这样做时:
for index, row in dataset.iterrows():
d = datetime.strptime(row['timestamp'], '%d/%m/%y %H.%M.%S')
row['timestamp'] = d.strftime('%y-%m-%d %H:%M:%s')
I get the error:我得到错误:
ValueError: time data '11/03/2004 00.00.00' does not match format '%d/%m/%y %H.%M.%S' ValueError:时间数据 '11/03/2004 00.00.00' 与格式 '%d/%m/%y %H.%M.%S' 不匹配
How can I solve it?我该如何解决? The format seems correct to me格式对我来说似乎正确
You're using the wrong year format, according to the documentation you should be using %Y
instead of %y
.根据您应该使用%Y
而不是%y
的文档,您使用了错误的年份格式。 %y
is for years without the century. %y
是没有世纪的年份。
from datetime import datetime
datetime.strptime('11/03/2004 00.00.00', '%d/%m/%Y %H.%M.%S')
Refer strftime() and strptime() Format Codes参考strftime() 和 strptime() 格式代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.