当前位置:主页 > 软件编程 > Python代码 >

python 时间信息“2018-02-04 18:23:35“ 解析成字典形式的结果代码详解

时间:2021-03-26 09:21:47 | 栏目:Python代码 | 点击:

将时间信息“2018-02-04  18:23:35“ 解析成字典形式的结果

:{‘year':2018,‘month':2,‘day':4,‘hour':18:‘minute':23,‘second':35}

这道题有许多解法,可以用正则匹配,可以用时间模块等诸多方法。今天我又用类方法把这题写了一次。

废话少说,上代码

#将时间信息“2018-02-04 18:23:35“ 解析成字典形式的结果,
# 如:{‘year':2018,‘month':2,‘day':4,‘hour':18:‘minute':23,‘second':35}
def t_time(*args):
  class Time():
    def __str__(self):
      return('year:{},month:{},day:{},hour:{},minute:{},second:{}'
          .format(self.y,self.m,self.d,self.h,self.M,self.s) )
  t=Time()
  t.y=2018;t.m=2;t.d=4
  t.h=18;t.M=23;t.s=35
  return t
print(t_time())

总结

您可能感兴趣的文章:

相关文章