python时间字符串格式不正确_Python时间数据与格式不匹配(Python time data does not match format)...

Python时间数据与格式不匹配(Python time data does not match format)

我有以下格式的字符串日期数据:

4/16/15 23:50

当我尝试转换为datetime对象时:

print datetime.datetime.strptime(fecha2, '%d/%m/%y %H:%M')

我收到此错误:

ValueError: time data '4/16/15 23:50' does not match format '%d/%m/%y %H:%M'

I have string date data with the following format:

4/16/15 23:50

When I try to convert into a datetime object:

print datetime.datetime.strptime(fecha2, '%d/%m/%y %H:%M')

I get this error:

ValueError: time data '4/16/15 23:50' does not match format '%d/%m/%y %H:%M'

原文:https://stackoverflow.com/questions/29926781

更新时间:2019-11-09 11:03

相关问答

你有月和日的逆转: print datetime.datetime.strptime(fecha2, '%m/%d/%y %H:%M')

you have month and day reversed: print datetime.datetime.strptime(fecha2, '%m/%d/%y %H:%M')

根据错误: ValueError: time data ' 00:59 03/20/1978' does not match format ' %H:%M %d/%m/%Y'

您正在尝试使用错误的日期格式。 具体来说,根据这一点,它试图将20解析为一个月( %m )。 我认为你想要的日期格式是' %H:%M %m/%d/%Y' (注意%m和%d已被切换)。 According to the error: ValueError: time data ' 00:59 03/20/1978' does

...

编辑: 正如约翰所提到的 ,让自己更容易,并删除前导和尾随空格。 另一个想法: 您当前的区域设置不能将“MAR”指定为月份缩写。 这段代码的输出结果是什么? import locale

locale.getdefaultlocale()

我在Linux机器上测试了你的代码(Ubuntu 9.10,Python 2.6.4)并且得到了ValueError。 我删除了空格,更改为非英文语言环境(捷克语),并得到了ValueError。 学术注释: 奇怪的是,您的代码在Windows XP Pytho

...

您可以随时解析文本,然后使用它自己构建日期时间 - 或者如果适合您,则预先验证它: from datetime import datetime

def to_datetime(date_string):

year = int(date_string[0:4])

month = int(date_string[5:7])

day = int(date_string[8:10])

return datetime(year, month, day)

print(to

...

你需要从格式字符串中删除逗号,然后它应该工作。 例如: >>> from datetime import datetime

>>> date = '26 March 2018'

# v v No comma here

>>> datetime.strptime(date, '%d %B %Y')

datetime.datetime(2018, 3, 26, 0, 0)

You need to remove comma , from

...

一般来说,我会动 ((dt-(datetime.datetime.strptime((user["access_key_1_last_rotated"]).split('T')[0],"%Y-%m-%d").date()))))

超出打印语句并将其分配给一个变量。 然后,快速和肮脏,你可以包裹在一个尝试除外 var my_date = "N/A"

try:

my_date = ((dt-(datetime.datetime.strptime((user["access_key_1_las

...

由于您的格式中包含空格,因此无法识别您的字符串。 这样可行: from datetime import datetime, date

date_of_birth = datetime.strptime("09121991", '%d%m%Y')

请注意,您不能使用input来输入09121991因为在Python 2中: input评估表达式 09121991被视为八进制数,但因包含9而无效 所以替代方法是使用raw_input() 。 那会有用。 顺便说一句:如果需要用零填充,请使用raw_

...

字符串中有一个前导空格: ' 1:00:00'

做这个: dfp['time'] = pd.to_datetime(dfp['time'].strip(), format='%H:%M:%S')

There's a leading space in the string: ' 1:00:00'

Do this: dfp['time'] = pd.to_datetime(dfp['time'].strip(), format='%H:%M:%S')

%m表示“月份为零填充十进制数”。 您的月份是July因此您应该使用%B ,即“月份作为区域设置的全名”。 参考: https : //docs.python.org/2/library/datetime.html#strftime-strptime-behavior %m means "Month as a zero-padded decimal number." Your month is July so you should use %B, which is "Month as locale

...

%m匹配月份表示为两位小数(在[01, 12] )。 使用%b表示缩写的月份名称,或使用%B表示完整的月份名称: fmt = '%a %d %b %Y %I:%M %p %Z'

可以在此处找到显示日期格式指令及其含义的表格。 如果您在使用%Z解析PDT遇到问题: 每个时间.strptime文档 : 对%Z指令的支持基于tzname中包含的值以及日光是否为真。 因此,它是特定于平台的,除了识别始终已知的UTC和GMT(并且被认为是非夏令时时区)。 因此,如果在没有PDT的情况下解析日期字符串,则:

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值