Home
Sign Up
Sign In
V2EX
›
问与答
Python 如何处理多个 format 格式?
coolair
·
Mar 10, 2017
· 2124 views
This topic created in 3532 days ago, the information mentioned may be changed or developed.
value = '2017-01-01'
datetime.strptime(value, '%Y-%m-%d')
value 可能是 '20170101', 也可能是 '201701'。
怎么添加多个 format ,一级一级去匹配。
value
Format
一级
2017-01-01
2 replies
•
2017-03-10 15:50:03 +08:00
xuboying
Mar 10, 2017 via Android
1
try catch
coolair
OP
Mar 10, 2017
2
formats = ['%Y-%m-%d', '%Y%m%d', '%Y%m']
for item in formats:
....try:
........datetime.strptime(value, item)
....except:
........pass
About
·
Help
·
Advertise
·
Blog
·
API
·
FAQ
·
Privacy
·
Solana
·
1099 Online
Highest 6679
·
Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 26ms ·
UTC 18:20
·
PVG 02:20
·
LAX 10:20
·
JFK 13:20
♥ Do have faith in what you're doing.
❯