Python之基本扩展模块

一、datetime模块

1.1 主要的模块 

datetime.date()    #处理日期(年、月、日)
datetime.time()    #处理时间(时、分、秒和毫秒)
datetime.datetime()    #处理日期+时间
datetime.timedelta()     #处理时段(时间间隔)
#获取今天的时间
print(datetime.date.today())
print(datetime.datetime.now())

print(datetime.date.today().strftime("%Y-%m-%d %H-%M-%S"))
print(datetime.datetime.now().isoformat())

  1.2 时间戳 

 时间戳是指格林威治时间1970年01月01日00时00分00秒到现在的总秒数。

1.2.1 将日期转换为时间戳以及将时间戳转换为日期

timetuple函数:将日期转化为struct_time格式

time.mktime函数:返回用秒数来表示时间的浮点数

import datetime,time

today = datetime.date.today()
print(today.timetuple())

print(time.mktime(today.timetuple()))

print(datetime.date.fromtimestamp(1668614400.0))

1.2.2 时间上的加减法 

 timedelta()方法表示两个时间点的间隔

import datetime
today = datetime.datetime.now()
print(today)

yesterday = today - datetime.timedelta(days=1)
print(yesterday)

hours = today - datetime.timedelta(hours=1)
print(hours)

二、Calendar模块 

 跟日历相关的若干函数和类,可以生成文本形式的日历。

2.1 常用函数

import calendar
calendar.calendar(<年>)       
calendar.month(<年>,<月>)        #返回多行字符串
calendar.prmonth(<年>,<月>)      #相当于print(calendar.month(<年>,<月>))
calendar.prcal(<年>)             #相当于print(calendar.calendar(<年>))
import calendar

print(calendar.month(2022,11))
print(calendar.calendar(2022))

输出结果展示: 

E:\ProgramData\Anaconda3\python.exe C:/Users/dl/PycharmProjects/MyProjects/1.py
   November 2022
Mo Tu We Th Fr Sa Su
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

                                  2022

      January                   February                   March
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
                1  2          1  2  3  4  5  6          1  2  3  4  5  6
 3  4  5  6  7  8  9       7  8  9 10 11 12 13       7  8  9 10 11 12 13
10 11 12 13 14 15 16      14 15 16 17 18 19 20      14 15 16 17 18 19 20
17 18 19 20 21 22 23      21 22 23 24 25 26 27      21 22 23 24 25 26 27
24 25 26 27 28 29 30      28                        28 29 30 31
31

       April                      May                       June
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
             1  2  3                         1             1  2  3  4  5
 4  5  6  7  8  9 10       2  3  4  5  6  7  8       6  7  8  9 10 11 12
11 12 13 14 15 16 17       9 10 11 12 13 14 15      13 14 15 16 17 18 19
18 19 20 21 22 23 24      16 17 18 19 20 21 22      20 21 22 23 24 25 26
25 26 27 28 29 30         23 24 25 26 27 28 29      27 28 29 30
                          30 31

        July                     August                  September
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
             1  2  3       1  2  3  4  5  6  7                1  2  3  4
 4  5  6  7  8  9 10       8  9 10 11 12 13 14       5  6  7  8  9 10 11
11 12 13 14 15 16 17      15 16 17 18 19 20 21      12 13 14 15 16 17 18
18 19 20 21 22 23 24      22 23 24 25 26 27 28      19 20 21 22 23 24 25
25 26 27 28 29 30 31      29 30 31                  26 27 28 29 30

      October                   November                  December
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
                1  2          1  2  3  4  5  6                1  2  3  4
 3  4  5  6  7  8  9       7  8  9 10 11 12 13       5  6  7  8  9 10 11
10 11 12 13 14 15 16      14 15 16 17 18 19 20      12 13 14 15 16 17 18
17 18 19 20 21 22 23      21 22 23 24 25 26 27      19 20 21 22 23 24 25
24 25 26 27 28 29 30      28 29 30                  26 27 28 29 30 31

2.2 将日历列表化 

calendar.monthcalendar()

返回某一年的某一个月份日历,是一个嵌套列表。最里层的列表含有七个元素,代表一周(从周一到周日)。如果没有本月的日期,则为0。 

import calendar

print(calendar.monthcalendar(2022,11))

输出结果:

[[0, 1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12, 13], [14, 15, 16, 17, 18, 19, 20], [21, 22, 23, 24, 25, 26, 27], [28, 29, 30, 0, 0, 0, 0]]

 2.3 判断闰年

calendar.isleap(<年>)
import calendar

print(calendar.isleap(2022))
print(calendar.isleap(2000))

输出结果:
False
True

  2.4 判断月数和周几

计算某月从周几开始,共有几天的时候,是从0开始,周一、周二……

计算某天是周几的时候,返回的是0-6,依次对应的是周一到周日。

import calendar

print(calendar.monthrange(2022,11))
print(calendar.weekday(2022,11,18))

输出结果:
(1, 30)
4

三、time模块 

3.1 获取时间戳 

time.time() 方法

可以计算某段程序的运行时间

import time

t1 = time.time()
a = 0
for i in range(10000):
    a += i
print(a)
t2 = time.time()
print(t2-t1)

 3.2 获取日期格式

3.2.1 获取当前时间 

import time

t1 = time.asctime()
t2 = time.ctime()
print(t1)
print(t2)

 3.2.2 将元组数据转化为日期

import time

t = (2022,11,18,11,8,30,0,0,0)
print(time.asctime(t))

输出结果:
Mon Nov 18 11:08:30 2022

 3.3 利用索引获取时间信息

import time

print(time.localtime())
t = time.localtime()
year = t[0]
print(year)

四、算术模块 

  4.1 random模块

 伪随机数:计算机中的随机函数是按照一定的算法模拟产生的,其结果是确定的,是可预见的。

随机数种子:随机数种子相同,随机数序列也是相同的。

random.seed(a = None)

random()      #生成范围在【0,1)之间的随机实数

uniform()       #生成指定范围内的随机浮点数

randint(m,n)      #生成指定范围【m,n】内的整数

randrange(a,b,n)        #可以在【a,b)范围内,按n递增的集合中随机选择一个数

getrandbits(k)             #生成k位二进制的随机整数

choice()           #从指定序列中随机选择一个元素

sample()          #能指定每次随机元素的个数

shuffle()        #可以将可变序列中所有元素随机排序

import random
colors = ['red','green','blue','yellow','black']
print(random.choice(colors))
print(random.sample(colors,3))
random.shuffle(colors)
print(colors)

输出结果:
black
['black', 'blue', 'green']
['red', 'blue', 'green', 'yellow', 'black']

五、文件文本读写模块 

5.1 文件的打开

open()函数:

f = open(filename[,mode[,buffering]])

f:open()返回的文件对象

filename:文件的字符串名

mode:可选参数,打开模式和文件类型 

buffering:可选参数,文件的缓冲区,默认为-1

文件的打开模式:

mode第一个字母表明对其的操作:

‘r’:表示读模式

‘w’:表示写模式

'x':表示在文件不存在的情况下新创建并写文件

‘a’:表示在文件末尾追加写内容

'+':表示读写模式

mode第二个字母是文件类型:

‘t’:表示文本类型

‘b’:表示二进制文件

5.2 文件的读写和访问 

文件的写操作:

f.write(str)

f.writelines(strlist):写入字符串列表

文件的读操作:

 f.read()

f.readline():返回一行

f.readlines():返回所有行、列表

f = open('mytxt','w')
f.writelines(['apple\n','orange\n'])
f.close()
f = open('mytxt','r')
print(f.readlines())
f.close()

 文件打开后一定要记得关闭,关闭的作用是终止对外部文件的连接,同时将缓存区的数据刷新到硬盘上。

5.3 结构化文本文件:CSV 

文件读取—reader

re = csv.reader()

接受一个可迭代对象(比如csv文件),能返回一个生成器,可以从其中解析出内容。

文件读取—DictReader

 与reader类似,适用于带有表头的csv文件,返回的每一个单元格都放在一个元组的值内。

文件写操作:
w = csv.writer()

w.writerow(rows)

当文件不存在时,自动生成,支持单行写入和多行写入

字典数据写入:

w = csv.DictWriter()

w.writeheader()

w.writerow(rows)

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小阿丁呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值