NameError: name ‘total_count’ is not defined或ValueError: malformed string
比如原始字符串是 str = '{"name":"jgy","age":null}'
转为字典时会发现null不是一个字符串,就当做变量,但是没有定义(python认可的是Null True False 注意第一个字母大写),所以出错,相应的实际过程中可能还会到 ‘true’ is not defined, ‘false’ is not defined类似的等等。
既然没定义,在前面定义一下就行了,比如自己定义 null = 'null', true = 'true'..
还有个想法,可以用正则把冒号后面的不是数字也没加引号的value值给加上引号。
获取最简洁日期格式:
>>>time.strftime('%Y-%m-%d',time.localtime(time.time()))
'2013-07-04'
获取上一秒时间:
time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time() - 1))
将一个字符串时间转为python时间格式
str1 = '2013-06-03 20:07:41' str2 = '2013-06-03 20:08:43' time1= time.strptime(str1,"%Y-%m-%d %H:%M:%S") #time1 is time_struct type time1= time.strptime(str2,"%Y-%m-%d %H:%M:%S") #now we wanna to 取得time1和time2的时间差 #需要转为datetime,然后使用datetime的timedelta方法 datetime1 = datetime.datetime(*time1[:6]) datetime2 = datetime.datetime(*time2[:6]) datedelta = datetime2 - datetime1 print datedelta #datetime.timedelta(0, 62) #后面是秒数 seconds = delta.seconds #获取int型秒数8
转载于:https://www.cnblogs.com/caroar/archive/2013/05/29/3106421.html
相关资源:数据结构—成绩单生成器