Python无效的格式字符串 [英] Python Invalid format string

查看:214
本文介绍了Python无效的格式字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用strftime以以下格式打印日期:
2007年6月3日05:40 PM

I am trying to print the date in the following format using strftime: 06-03-2007 05:40PM

我编写了以下代码:

import time
print time.strftime("%m-%d-%Y %T:%M%p")

但它给出了错误无效格式字符串。为什么?

But it gives an error "Invalid format string". Why is that?

推荐答案

也许您正在寻找小时 H 而不是 T 。检查 strftime 可能的选项

Maybe you are looking for hour, H instead of T. check strftime possible options

print time.strftime("%m-%d-%Y %H:%M%p")

注意
Windows和Linux计算机上的行为不同。如果您看到Windows平台支持的功能,请检查此 http:// msdn。 microsoft.com/en-us/library/fe06s4ak.aspx

因此最好使用%H:%M:在Windows上,%S %T 大。

这篇关于Python无效的格式字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆