简体   繁体   English

在 python 中将日期从 DD/MM/YYYY HH.mm.ss 转换为 MM-DD-YYYY HH:MM:SS 时出错

[英]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.

相关问题 转换(YYYY-MM-DD-HH:MM:SS)日期时间 - Converting (YYYY-MM-DD-HH:MM:SS) date time 使用 python 将日期格式 mm/dd/yyyy hh:mm:ss AM 更改为 yyyy-mm-dd hh:mm:ss 格式 - change date format mm/dd/yyyy hh:mm:ss AM to yyyy-mm-dd hh:mm:ss format using python 将字符串 MM-dd-yyyy hh:mm:ss 转换为 PySpark 中的时间戳 yyyy-MM-dd hh:mm:ss - Convert string MM-dd-yyyy hh:mm:ss to timestamp yyyy-MM-dd hh:mm:ss in PySpark Python:如何将 UTC 时间戳从 mm/dd/yyyy hh:mm:ss AM 更改为 dd/mm/yyyy hh:mm:ss AM - Python: How to change a UTC timestamp from mm/dd/yyyy hh:mm:ss AM to dd/mm/yyyy hh:mm:ss AM Python 日期时间转换格式为 dd/mm/yyyy HH:MM:SS - Python Datetime convert format into dd/mm/yyyy HH:MM:SS 从Pandas Python中的DD-MON-YYYY hh.mm.ss AM / PM [Oracle Date]中提取周,月 - extract week, month from DD-MON-YYYY hh.mm.ss AM/PM [Oracle Date] in Pandas Python 如何将 csv 文件中的时间格式从 DD:MM:YY HH:MM 更改为 YYYY-MM-DD HH:MM:SS。 或 YYYY/MM/DD HH:MM:SS - How to change time format in a csv file from DD:MM:YY HH:MM to YYYY-MM-DD HH:MM:SS. or YYYY/MM/DD HH:MM:SS 将日期格式从 csv. 文件中的 python 从 YYYY-MM-DD HH:MM:SS+00:00 转换为 YYYY-MM-DD - Converting a date format from a csv. file in python from YYYY-MM-DD HH:MM:SS+00:00 to YYYY-MM-DD Python Pandas 将日期时间格式从 'MM/DD/YYYY hh:mm:ss' 转换为 'YYYY-MM-DDThh:mm:ssZ' - Python Pandas to convert datetime format from 'MM/DD/YYYY hh:mm:ss' to 'YYYY-MM-DDThh:mm:ssZ' Python:如何将 UTC 时间戳从 mm/dd/yyyy hh:mm:ss AM 更改为 dd/mm/yyyy hh:mm:ss AM - Python: How can I change a UTC timestamp from mm/dd/yyyy hh:mm:ss AM to dd/mm/yyyy hh:mm:ss AM
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM