Python - 模块
模块,用一些代码实现了某个功能的代码集合。
类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合。而对于一个复杂的功能来,可能需要多个函数才能完成(函数又可以在不同的.py文件中),n个 .py 文件组成的代码集合就称为模块。
如:os 是系统相关的模块;
模块分为三种:- 自定义模块
- 第三方模块
- 内置模块
一、time模块
>>> import time >>> time.sleep(1) #睡,后面指定秒数。 >>> time.time() #返回当前时间的时间戳,每个设备的时间戳都是从1970年1月1日开始算起 1503076001.620359 >>> time.ctime() #返回当前日期时间,字符串格式 'Fri Aug 18 19:12:28 2017' >>> time.gmtime() #得到struct_time格式的年月日,时分秒的数值,这个是格林威治时间 time.struct_time(tm_year=2017, tm_mon=8, tm_mday=18, tm_hour=17, tm_min=13, tm_sec=36, tm_wday=4, tm_yday=230, tm_isdst=0) >>> time.localtime() #当地时间 time.struct_time(tm_year=2017, tm_mon=8, tm_mday=18, tm_hour=19, tm_min=15, tm_sec=45, tm_wday=4, tm_yday=230, tm_isdst=1) >>> time.mktime(time.localtime()) #把struct_time格式的时间,转换为时间戳 1503076638.0 >>> time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) #把struct_time格式的时间转换为字符串格式 '2017-08-18 17:18:38' >>> time.strptime("2017-08-18 19:19","%Y-%m-%d %H:%M") #把字符串格式的时间转换为struct_time格式 time.struct_time(tm_year=2017, tm_mon=8, tm_mday=18, tm_hour=19, tm_min=19, tm_sec=0, tm_wday=4, tm_yday=230, tm_isdst=-1)
二、datetime模块
>>> import datetime >>> datetime.date.today() #输入当天日期 2017-08-18 >>> datetime.datetime.now() #输出当前时间 2017-08-18 19:24:03.632905 >>> datetime.datetime.now().timetuple() #转换为struct_time格式 time.struct_time(tm_year=2017, tm_mon=8, tm_mday=18, tm_hour=19, tm_min=25, tm_sec=48, tm_wday=4, tm_yday=230, tm_isdst=-1) >>> datetime.date.fromtimestamp(time.time()) #把时间戳格式转为日期 2017-08-18 >>> datetime.datetime.now().replace(2018,9,13) #替换为指定时间 2018-09-13 19:28:59.757380 >>> datetime.datetime.strptime("2017-08-18 17:26","%Y-%m-%d %H:%M") 2017-08-18 17:26:00 new_date = datetime.datetime.now() + datetime.timedelta(days=10) #比现在加10天 print(new_date) 2017-08-28 19:35:49.095399 new_date = datetime.datetime.now() + datetime.timedelta(days=-10) #比现在减10天 print(new_date) 2017-08-08 19:35:49.095433 new_date = datetime.datetime.now() + datetime.timedelta(hours=-10) #比现在减10小时 print(new_date) 2017-08-18 09:35:49.095441 new_date = datetime.datetime.now() + datetime.timedelta(seconds=120) #比现在加120秒 print(new_date) 2017-08-18 19:37:49.095447
三、sys模块
sys.argv #获取给脚本传入的参数 sys.path #获取路径 sys.exit() #退出程序 >>> sys.version #python解释器版本信息 '3.6.1 |Anaconda 4.4.0 (x86_64)| (default, May 11 2017, 13:04:09) \n[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]' >>> sys.maxsize #返回最大值int 9223372036854775807 >>> import sys #返回操作系统平台名称 >>> sys.platform 'darwin' sys.stdout.write('#') 打印写入的数据,不会换行,(可以用来模拟进度条), for i in range(31): sys.stdout.write('\r') #每次先清空原来数据 sys.stdout.write('%s%% | %s' % (int(i/30*100),int(i/30*100)*'#')) sys.stdout.flush() #强制刷新到屏幕 time.sleep(0.2) #睡两秒 >>> 100% | #################################################################################################### val = sys.stdin.readline()[:1] #读取输入内容
四、pickle & json模块
用于序列化的两个模块
json,用于字符串 和 python数据类型间进行转换
pickle,用于python特有的类型 和 python的数据类型间进行转换
Json模块提供了四个功能:dumps、dump、loads、load
pickle模块提供了四个功能:dumps、dump、loads、load
f.write(pickle.dumps(x)) 等同于 pickle.dump(x,f) #把x写入f
pickle.loads(f.read()) 等同于 pickle.load(f) 读取f文件
#定义一些数据,将其写入一个文件 import pickle accounts={ 1000:{ 'name':'Alex', 'age':19, 'sexy':'male', 'balance':15000, 'email':'alex1997@gmail.com', 'phone':'0752858987' }, 1001:{ 'name':'Kevin', 'age':29, 'sexy':'male', 'balance':20000, 'email':'kevin87@gmail.com', 'phone':'0798548987' } } with open('account.db','wb') as f: f.write(pickle.dumps(accounts)) #把上面数据写入到account.db文件中
#去购物,Alex消费了500,并修改原数据 import pickle with open('account.db','rb') as f: r = pickle.load(f) #等同pickle.loads(f.read) #加载字典数据 print(r) r[1000]['balance'] -= 500 #扣钱 with open('account.db','wb') as f: pickle.dump(r,f) #等同f.write(pickle.dumps(r)) #然后重新写入,
#检查数据书否被修改 import pickle with open('account.db','rb') as f: f = pickle.load(f) #加载字典数据 print(f)
评论
发表评论