常用模块

一、时间模块

使用前必须先导入time模块

1.时间戳

print(time.time())

2.结构化时间

print(time.localtime())(本地时间)

print(time.gmtime()) (格林威治时间)

3.时间提取(年月日时分秒)

print(time.localtime().tm_year)

4.格式化字符串

print(time.strftime('%Y-%m-%d %H:%M:%S'))
print(time.strftime('%Y-%m-%d %X'))

二、random模块

1.随机获取一个ip作为代理

proxy_ip=[
'1.1.1.1',
'1.1.1.2',
'1.1.1.3',
'1.1.1.4',
]

print(random.choice(proxy_ip))

2.随机产生验证码

def v_code(n=5):
res=''
for i in range(n):
num=random.randint(0,9)
s=chr(random.randint(65,90))
add=random.choice([num,s])
res+=str(add)
return res

print(v_code(6))

三、os模块

1、文件绝对路径

print(os.path.abspath('a/b/c.txt')) 
print(os.path.abspath('/a/b/c.txt'))

2.文件目录分割

print(os.path.split('E:\\a\\c.txt'))

3.文件所在目录

print(os.path.dirname('E:\\a\\c.txt'))
print(os.path.dirname('E:\\a'))

4.返回文件名

print(os.path.basename('E:\\a\\a,txt'))

5.判断文件是否存在

print(os.path.exists('E:\\a'))
print(os.path.exists('E:\wupeiqi\s17\day06'))

6.判断路径是否为绝对路径

print(os.path.isabs('E:\\day06'))
print(os.path.isabs('day06'))

7.获取文件大小

print(os.path.getsize(r'E:\wupeiqi\s17\day06\test.py'))

8.拼接为一个路径

print(os.path.join('a','E:\\b','c.txt'))

9.上一级目录

print(os.path.normpath('c://windows\\System32\\../Temp/'))

四、shutil模块 tarfile模块

1.复制文件

shutil.copyfileobj(open('test.py','r'),open('test1.py','w'))

shutil.copyfile('test1.py','test2.py')

2.文件归档

shutil.make_archive("data_bak", 'gztar', root_dir=r'E:\wupeiqi\s17\day06\bbb')

# import tarfile
# t=tarfile.open('data_bak.tar.gz','r')
# t.extractall('extract_dir')
# t.close()

# import tarfile
# t=tarfile.open('egon1.tar','w')
# t.add(r'E:\wupeiqi\s17\day06\bbb\b.py',arcname='a.bak')
# t.add(r'E:\wupeiqi\s17\day06\bbb\spam.py',arcname='b.bak')
# t.close()

# t=tarfile.open('egon1.tar','r')
# t.extractall('extract_dir2')

 五、json&pickle模块

序列化模拟

dic={
'name':'alex',
'age':9000,
'height':'150cm',
}
with open('a.txt','w') as f:
f.write(str(dic))


with open('a.txt','r') as f:
res=eval(f.read())
print(res['name'],type(res))

eval读出字符串内容,在当前位置执行

 

json将统一的对象序列化为python识别的对象

非序列化

# x="[null,true,false,1]"
# x="[None,True,1]"

# res=eval(x)
#
# print(res[0],type(res))

反序列化

# import json
# res=json.loads(x)
# print(res)

 

#序列化的过程:dic---->res=json.dumps(dic)---->f.write(res)

import json
dic={
'name':'alex',
'age':9000,
'height':'150cm',
}

res=json.dumps(dic)
print(res,type(res))
with open('a.json','w') as f:
f.write(res)

 

#反序列化的过程:res=f.read()---->res=json.loads(res)---->dic=res

import json
with open('a.json','r') as f:
dic=json.loads(f.read())
print(dic,type(dic))
print(dic['name'])


#json的便捷操作

import json
dic={
'name':'alex',
'age':9000,
'height':'150cm',
}
json.dump(dic,open('b.json','w')) #转换的同事直接往文件写

 

import json

res=json.load(open('b.json','r'))
print(res,type(res))

pickle模块

import pickle

dic={'name':'alex','age':13}

print(pickle.dumps(dic)) #将文件内容转换为bytes类型

序列化

with open('a.pkl','wb') as f:
     f.write(pickle.dumps(dic))

反序列化

with open('a.pkl','rb') as f:
    d=pickle.loads(f.read())
    print(d,type(d))

 

# dic={'name':'alex','age':13}
# pickle.dump(dic,open('b.pkl','wb'))
# res=pickle.load(open('b.pkl','rb'))
# print(res,type(res))

序列化python任意对象

import json
import pickle
# def func():
# print('from func')

# json.dumps(func)# 报错,json不支持python的函数类型
# f=pickle.dumps(func)
# print(f)

# pickle.dump(func,open('c.pkl','wb'))
# res=pickle.load(open('c.pkl','rb'))
# print(res)
# res()

反序列化(先前序列化的是内存地址)

import pickle

pickle.load(open('c.pkl','rb'))

posted @ 2017-06-09 17:52  geek_ace  阅读(261)  评论(0编辑  收藏  举报