博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python学习-3.一些常用模块用法
阅读量:4327 次
发布时间:2019-06-06

本文共 2821 字,大约阅读时间需要 9 分钟。

一、time、datetime

时间戳转化为元组

1 >>> time.localtime()2 time.struct_time(tm_year=2016, tm_mon=8, tm_mday=26, tm_hour=16, tm_min=21, tm_sec=38, tm_wday=4, tm_yday=239, tm_isdst=0)3 >>> time.gmtime()4 time.struct_time(tm_year=2016, tm_mon=8, tm_mday=26, tm_hour=8, tm_min=21, tm_sec=44, tm_wday=4, tm_yday=239, tm_isdst=0)
元组转化为时间戳

1 >>> a =time.localtime()2 >>> a3 time.struct_time(tm_year=2016, tm_mon=8, tm_mday=26, tm_hour=16, tm_min=23, tm_sec=31, tm_wday=4, tm_yday=239, tm_isdst=0)4 >>> time.mktime(a)5 1472199811.0
元组和格式化
>> atime.struct_time(tm_year=2016, tm_mon=8, tm_mday=26, tm_hour=16, tm_min=23, tm_sec=31, tm_wday=4, tm_yday=239, tm_isdst=0)>>> time.strptime('2016-12-12 12:12:12',"%Y-%m-%d %H:%M:%S")time.struct_time(tm_year=2016, tm_mon=12, tm_mday=12, tm_hour=12, tm_min=12, tm_sec=12, tm_wday=0, tm_yday=347, tm_isdst=-1)>>> time.strftime("%Y-%m-%d %H:%M:%S",a) '2016-08-26 16:23:31'
元组转化为字符串

1 >>> a= time.localtime()2 >>> a3 time.struct_time(tm_year=2016, tm_mon=8, tm_mday=26, tm_hour=16, tm_min=26, tm_sec=42, tm_wday=4, tm_yday=239, tm_isdst=0)4 >>> time.asctime(a)5 'Fri Aug 26 16:26:42 2016'
时间戳转化为字符串

1 >>> a = time.mktime(time.localtime())2 >>> a3 1472200092.04 >>> time.ctime(a)5 'Fri Aug 26 16:28:12 2016'
datatime

1 >>> datetime.datetime.now()2 datetime.datetime(2016, 8, 26, 16, 29, 6, 74390)3 >>> datetime.datetime.now() + datetime.timedelta(-4)4 datetime.datetime(2016, 8, 22, 16, 29, 12, 625394)5 >>> datetime.datetime.now() + datetime.timedelta(hours=9)6 datetime.datetime(2016, 8, 27, 1, 29, 16, 201594)
二、random模块

random.randint(1,9) 1和9都在random.randrange(1,8)	8不在random.random()	0-1random.uniform(1,10)random.randrange(1,8)random.choice('hellp') 从中随机选一个random.sample('hello',2)>>> a=[1,2,3,4,5]>>> random.shuffle(a)>>> a[4, 1, 5, 3, 2]

三、os模块

>>> os.getcwd()'/root/oldboy'>>> os.chdir('..')>>> os.getcwd()'/root'>>>os.chdir(r'')>>> os.curdir'.'>>> os.pardir'..'os.makedirs()	多级目录os.removedirs()	删除多级目录,删除后上一级目录为空,照样删除os.mkdir()	只能创建单级目录os.rmdir()	只删除单级os.listdir()	列出当前目录os.rename(old,new)os.stat() os.sepos.pathsepos.linesep>>> os.name'posix'os.system()os.environos.path.abspath(path)>>> os.path.split(r'/root/1.c')('/root', '1.c')>>> os.path.basename('/root/1.c') '1.c'>>> os.path.dirname('/root/1.c')'/root'>>> os.path.exists('/root') Trueos.path.isabs('/root/1.c')os.path.isfile()	是否是文件os.path.isdir()os.path.join(['',''])	多个路径组合返回os.path.getatime()os.path.getmtime()

四、shutil模块

shutil.rmtree()

shutil.copytree('test','newtest')
shutil.copystat()
shutil.copyfile()
shutil.move()
shutil.make_archive(n)

五、shelve模块

import shelved = shelve.open('shelve_test') #打开一个文件class Test(object):def __init__(self,n):self.n = nt = Test(123) t2 = Test(123334)name = ["alex","rain","test"]d["test"] = name #持久化列表d["t1"] = t #持久化类d["t2"] = t2d.close()

转载于:https://www.cnblogs.com/weikunzz/p/6710535.html

你可能感兴趣的文章
Centos 7 Mysql 最大连接数超了问题解决
查看>>
thymeleaf 自定义标签
查看>>
关于WordCount的作业
查看>>
C6748和音频ADC连接时候的TDM以及I2S格式问题
查看>>
UIView的layoutSubviews,initWithFrame,initWithCoder方法
查看>>
STM32+IAP方案 实现网络升级应用固件
查看>>
用74HC165读8个按键状态
查看>>
jpg转bmp(使用libjpeg)
查看>>
linear-gradient常用实现效果
查看>>
sql语言的一大类 DML 数据的操纵语言
查看>>
VMware黑屏解决方法
查看>>
JS中各种跳转解析
查看>>
JAVA 基础 / 第八课:面向对象 / JAVA类的方法与实例方法
查看>>
Ecust OJ
查看>>
P3384 【模板】树链剖分
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
Equivalent Strings
查看>>
flume handler
查看>>