python学习笔记(5)json和time模块

json:

import json
'''
# json是一种数据交换格式,是语言跟语言之间的沟通介质
# 序列化:将本语言支持的数据对象转换成json格式的字符串
# 反序列化:将json格式的字符串转换成本语言支持的数据对象
json格式:
'123'  # 数字
'true'  # 布尔值,需要首字母小写
'[]'  # 数组
'{}'  # 字典
注意:只支持双引号
'''
info = {'name':'aa', 'age':10}
ret = json.dumps(info)
print(type(ret))
ret = json.loads(ret)
print(type(ret))

with open("a.txt",'w',encoding='utf8') as f:
    f.write(json.dumps(info))

with open("a.txt",'r',encoding='utf8') as f:
    text = f.read()
    print(repr(text))
    text1 = json.loads(text)
    print(type(text1),text1)

time:

import time,datetime
#时间戳
timestamp = time.time()

# 格式化时间
time_str = time.strftime('%Y/%m/%d %H:%M:%S')
time_str = time.strftime('%Y/%m/%d %X')
print(time_str)

#时间元组
time_tuple = time.localtime()
print(time_tuple)

t1 = time.localtime()
t2 = time.strftime('%Y/%m/%d %H:%M:%S %a')
print(t2)

# 字符串时间---->结构化时间
t3 = time.strptime('2021-05-22','%Y-%m-%d')
print(t3)

d1 = datetime.datetime.now()
print(d1)
d2 = datetime.datetime.today()
print(d2)
# 格式化时间
d3 = d1.strftime('%Y-%m-%d')
print(d3)
# 只输出日期
d4 = datetime.date.today()
print(d4)

d5 = d1 + datetime.timedelta(days = 7)
print(d5)

before_one_week_date = (datetime.datetime.now()-datetime.timedelta(days=7)
print(before_one_week_date)

 

posted on 2021-05-22 17:29  torotoise512  阅读(92)  评论(0编辑  收藏  举报