一,time模块学习

 import time

# print(int(time.time()))#时间戳
# res = time.strftime('%Y-%m-%d %H:%M:%S')#取当前格式化好的时间
# print(res)

#时间戳转换成时间元组,时间戳转格式化好的时间
#time1 = time.gmtime(int(time.time()))#把时间戳转成时间元组,以标准时间的时间转换的
# time1 = time.localtime(int(time.time()))#把时间戳转成时间元组,以标准时间的时间转换的
# res = time.strftime('%Y-%m-%d %H:%M:%S',time1)
# print(res)


#格式化好的时间转时间戳

timep = time.strptime('2018-10-23 15:38:59','%Y-%m-%d %H:%M:%S')
print(timep)
res = time.mktime(timep)#把时间元组转成时间戳
print(res)
#20181023 2323
def timestampToStr(timestamp=None,format='%Y-%m-%d %H:%M:%S'):
#时间戳转格式化好的时间
if timestamp:
time1 = time.localtime(timestamp)
res = time.strftime(format, time1)
else:
res = time.strftime(format)
return res
#20180304153958
def strTotimestamp(str=None,format='%Y%m%d%H%M%S'):
#格式化的时间转时间戳
if str:
timep = time.strptime(str, format)
res = time.mktime(timep)
else:
res = time.time()
return int(res)

二,json模块学习
import json
#解析json的

# json_str = '''
# {"name":"xiaohei","age":18,"sex":"男","age":18}
# '''
# res = json.loads(json_str) #把字符串(json串)转成字典
# print(res)
# print(type(json_str))
# print(type(res))

dic = {
"xiaohei":{
"age":18,
"password":12345,
"sex":"男",
"addr":"北京"
},
"马春波":{
"age":18,
"password":12345,
"sex":"男",
"addr":"北京"
},
"王东泽":{
"age":18,
"password":12345,
"sex":"男",
"addr":"北京"
},
}
# res = json.dumps(dic,ensure_ascii=False,indent=4)#把字典变成字符串
# print(res)
# f = open('user.json','w',encoding='utf-8')
# f.write(res)

#load 自己读
# f = open('user.json',encoding='utf-8')
# res = json.loads(f.read())
# print(res)
# res = json.load(f)
# print(res)

#dump 自己写的
fw = open('newuser.json','w')
json.dump(dic,fw,indent=4,ensure_ascii=False)

三,os模块学习

import os
res = os.listdir('/Users/nhy/Desktop') #列出某个目录下的所有文件
# os.remove()
# os.rename()
# os.mkdir(r'test/niuhy/haha')#创建文件夹
# os.makedirs(r'test1/lyl/aaa')#会创建父目录
# res = os.path.exists(r'/Users/nhy/Desktop/stu.txt')
# os.path.isfile() #判断是否为文件
# os.path.isdir()#判断是否为文件
# res = os.path.split(r'/Users/nhy/Desktop/stu.txt')
# res = os.path.dirname(r'/Users/nhy/Desktop/stu.txt')#取父目录
# res = os.getcwd() #获取当前的目录
# os.chdir(r'/Users/nhy/Desktop/')#更改当前目录
# res = os.getcwd()
# print(res)
# open('a.txt','w')
# print(os.environ)#看你电脑的环境变量
#
# res = os.path.join('test','hhh','abc','a.txt')#拼接路径
# print(res)
# res= os.path.abspath('..')#根据相对路径取绝对路径
# print(res)

# res = os.system('hhhsdf')#执行操作系统命令
# # print(res)
# res = os.popen('ifconfig').read()
# print('res',res)
posted on 2018-12-15 17:30  yulinlin_coding  阅读(160)  评论(0编辑  收藏  举报