码迷,mamicode.com
首页 > 编程语言 > 详细

16-Python-常用模块

时间:2017-12-07 23:56:29      阅读:396      评论:0      收藏:0      [点我收藏+]

标签:程序   either   cloc   python   避免   imp   ros   定义   clock   

1、模块的定义

模块:用来从逻辑上组织python代码(变量、函数、类、逻辑:实现一个功能),本质就是.py文件(例如文件名:test.py,对应的模块名则为test)。
包:用来从逻辑上组织模块,本质就是一个目录(必须带有一个__init__.py文件)
import模块的本质:导入模块的本质就是把python文件解释一遍。
import包的本质:导入包的本质就是解释包下面的__init__.py文件。
 1 import module_druid  # 实质是把module_druid中的所有代码都解释了(当前程序中执行)一便,然后赋值给了module_druid。
 2 
 3 print(module_druid.name)  # 调用模块中变量的方法。该方法需要加模块名名。
 4 
 5 module_druid.func1()  # 调用模块中的方法
 6 
 7 print("华丽的分隔符".center(30, "~"))
 8 
 9 from module_druid import name, func1  # 实质仅是把module_druid模块中的name变量,func1()放到当前程序中执行,而不是全部。
10 
11 print(name)  # 该方法不需要再加模块名
12 func1()  # 该方法不需要再加模块名
13 
14 print("华丽的分隔符".center(30, "~"))
15 
16 from module_druid import *  # 导入模块中所有的代码,在当前程序中执行。不建议这么使用,因为如果友重复的方法,会导致当前方法覆盖导入模块的方法。
17 
18 from module_druid import func2 as druid_func2  # 取别名,避免重复
19 druid_func2()
20 
21 print("华丽的分隔符".center(30, "~"))

 

2、time、datetime模块

2.1、 模块的语法

 1 import time
 2 
 3 
 4 print(time.clock())  # 返回处理器时间,3.3开始已废弃 , 改成了time.process_time()测量处理器运算时间,不包括sleep时间,不稳定,mac上测不出来
 5 print(time.altzone)   # 返回与utc时间的时间差,以秒计算\
 6 print(time.asctime())  # 返回时间格式"Fri Aug 19 11:14:16 2016",
 7 print(time.localtime())  # 返回本地时间 的struct time对象格式
 8 print(time.gmtime(time.time()-800000))  # 返回utc时间的struc时间对象格式
 9 
10 print(time.asctime(time.localtime()))  # 返回时间格式"Fri Aug 19 11:14:16 2016",
11 print(time.ctime())  # 返回Fri Aug 19 12:38:29 2016 格式, 同上
12 
13 
14 # 日期字符串 转成  时间戳
15 string_2_struct = time.strptime("2016/05/22", "%Y/%m/%d")  # 将日期字符串 转成 struct时间对象格式
16 print(string_2_struct)
17 
18 struct_2_stamp = time.mktime(string_2_struct)  # 将struct时间对象转成时间戳
19 print(struct_2_stamp)
20 
21 
22 # 将时间戳转为字符串格式
23 print(time.gmtime(time.time()-86640))  # 将utc时间戳转换成struct_time格式
24 print(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()))  # 将utc struct_time格式转成指定的字符串格式
25 
26 
27 #时间加减
28 import datetime
29 
30 print(datetime.datetime.now())  # 返回 2016-08-19 12:47:03.941925
31 print(datetime.date.fromtimestamp(time.time()))  # 时间戳直接转成日期格式 2016-08-19
32 print(datetime.datetime.now())
33 print(datetime.datetime.now() + datetime.timedelta(3))  # 当前时间+3天
34 print(datetime.datetime.now() + datetime.timedelta(-3))  # 当前时间-3天
35 print(datetime.datetime.now() + datetime.timedelta(hours=3))  # 当前时间+3小时
36 print(datetime.datetime.now() + datetime.timedelta(minutes=30))  # 当前时间+30分
37 
38 
39 c_time  = datetime.datetime.now()
40 print(c_time.replace(minute=3, hour=2))  # 时间替换

 

2.2、 参数解释     

DirectiveMeaningNotes
%a Locale’s abbreviated weekday name.  
%A Locale’s full weekday name.  
%b Locale’s abbreviated month name.  
%B Locale’s full month name.  
%c Locale’s appropriate date and time representation.  
%d Day of the month as a decimal number [01,31].  
%H Hour (24-hour clock) as a decimal number [00,23].  
%I Hour (12-hour clock) as a decimal number [01,12].  
%j Day of the year as a decimal number [001,366].  
%m Month as a decimal number [01,12].  
%M Minute as a decimal number [00,59].  
%p Locale’s equivalent of either AM or PM. (1)
%S Second as a decimal number [00,61]. (2)
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. (3)
%w Weekday as a decimal number [0(Sunday),6].  
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. (3)
%x Locale’s appropriate date representation.  
%X Locale’s appropriate time representation.  
%y Year without century as a decimal number [00,99].  
%Y Year with century as a decimal number.  
%z Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].  
%Z Time zone name (no characters if no time zone exists).  
%% A literal ‘%‘ character.

 

2.3、 时间格式之间的转换

技术分享图片


 

 1 import time
 2 # --------------------------按图1转换时间
 3 # localtime([secs])
 4 # 将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。
 5 time.localtime()
 6 time.localtime(1473525444.037215)
 7 
 8 # gmtime([secs]) 和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。
 9 
10 # mktime(t) : 将一个struct_time转化为时间戳。
11 print(time.mktime(time.localtime()))#1473525749.0
12 
13 
14 # strftime(format[, t]) : 把一个代表时间的元组或者struct_time(如由time.localtime()和
15 # time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。如果元组中任何一个
16 # 元素越界,ValueError的错误将会被抛出。
17 print(time.strftime("%Y-%m-%d %X", time.localtime()))#2016-09-11 00:49:56
18 
19 # time.strptime(string[, format])
20 # 把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。
21 print(time.strptime(2011-05-05 16:37:06, %Y-%m-%d %X))
22 # time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6,
23 #  tm_wday=3, tm_yday=125, tm_isdst=-1)
24 # 在这个函数中,format默认为:"%a %b %d %H:%M:%S %Y"。

 

 

 

 

16-Python-常用模块

标签:程序   either   cloc   python   避免   imp   ros   定义   clock   

原文地址:http://www.cnblogs.com/Druidchen/p/8001380.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!