Python入门-模块1(模块导入与time模块)

---恢复内容开始---

模块

一、模块分类:

模块分为三种:

1、内置模块:Python自带的标准模块(可使用help('modules’)查看Python自带模块列表)

2、第三方开源模块:可以通过pip install xxxx(或python3 -m pip install xxxx)联网安装

3、自定义模块

二、模块调用:

 几种常用的模块调用方式

1、import modules

2、from modules import xx

3、from modules.xx.xx import xx as rename

4、from modules.xx.xx import *

注:模块一旦被调用,相当于执行了另外一个.py文件里的代码

三、包

当模块文件较多时,需要将模块进行划分。比如将相同类型的,如数据库相关的放一个文件夹中,像这种管理多个模块的文件夹,我们称之为包

包就是文件夹,但该文件夹下必须存在一个__init__.py的文件,该文件可以为空,但必须存在。

四、time模块

 1、导入模块:

>>> import time
>>> time.time()
1523537181.252901

2、time模块

a  time.localtime() #将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。

>>> time.localtime()
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=12, tm_hour=20, tm_min=48, tm_s
ec=38, tm_wday=3, tm_yday=102, tm_isdst=0)
索引(Inde属性(Attribute)    值(Values)
0     tm_year(年)            比如2011 
1     tm_mon(月)             1 - 12
2     tm_mday(日)            1 - 31
3     tm_hour(时)            0 - 23
4     tm_min(分)             0 - 59
5     tm_sec(秒)             0 - 61
6     tm_wday(weekday)       0 - 6(0表示周日)
7     tm_yday(一年中的第几天)1 - 366
8     tm_isdst(是否是夏令时) 默认为-1

 b  time.time() #可以带时间戳参数 也不带。不带时以当前时间为准

>>> time.time()
1523537181.252901

c  time.mktime() #将一个struct_time转化为时间戳

>>> time.localtime()
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=12, tm_hour=20, tm_min=48, tm_s
ec=38, tm_wday=3, tm_yday=102, tm_isdst=0)
>>> a = time.localtime()
>>> time.mktime(a)
1523537567.0

d  time.strftime()  #将struct_time转为格式化时间格式输出

>>> time.strftime('%Y-%m-%d %H:%M:%S')
'2018-04-12 21:01:40'
字符串转时间格式对应表
Meaning    Notes
%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.

e  time.strptime() #time.strftime的逆操作

>>> time.strptime('2018-04-12 21:01:40','%Y-%m-%d %H:%M:%S')
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=12, tm_hour=21, tm_min=1, tm_se
c=40, tm_wday=3, tm_yday=102, tm_isdst=-1)

f  时间戳与格式化日期转换关系图

  

原文地址:https://www.cnblogs.com/fany-mok/p/8735234.html