Python常用模块-20个常用模块总结

time模块

time模块中时间表现的格式主要有三种:
  a、timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量
  b、struct_time时间元组,共有九个元素组。
  c、format time 格式化时间,已格式化的结构使时间更具可读性。包括自定义格式和固定格式。
1、时间格式转换图:

2、主要time生成方法和time格式转换方法实例:

#! /usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "TKQ"

import time

# 生成timestamp
time.time()
# 1477471508.05#struct_time to timestamptime.mktime(time.localtime())

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

struct_time元组元素结构

属性                            值
tm_year(年)                  比如2011 
tm_mon(月)                   1 - 12
tm_mday(日)                  1 - 31
tm_hour(时)                  0 - 23
tm_min(分)                   0 - 59
tm_sec(秒)                   0 - 61
tm_wday(weekday)             0 - 60表示周日)
tm_yday(一年中的第几天)        1 - 366
tm_isdst(是否是夏令时)        默认为-1

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

format time结构化表示

格式含义
格式含义
%a本地(locale)简化星期名称
%A本地完整星期名称
%b本地简化月份名称
%B本地完整月份名称
%c本地相应的日期和时间表示
%d一个月中的第几天(01 - 31)
%H一天中的第几个小时(24小时制,00 - 23)
%I第几个小时(12小时制,01 - 12)
%j一年中的第几天(001 - 366)
%m月份(01 - 12)
%M分钟数(00 - 59)
%p本地am或者pm的相应符
%S秒(01 - 61)
%U一年中的星期数。(00 - 53星期天是一个星期的开始。)第一个星期天之前的所有天数都放在第0周。
%w一个星期中的第几天(0 - 6,0是星期天)
%W和%U基本相同,不同的是%W以星期一为一个星期的开始。
%x本地相应日期
%X本地相应时间
%y去掉世纪的年份(00 - 99)
%Y完整的年份
%Z时区的名字(如果不存在为空字符)
%%‘%’字符

常见结构化时间组合:

print time.strftime("%Y-%m-%d %X")
#2016-10-26 20:50:13

 
 
  • 1
  • 2

3、time加减

#timestamp加减单位以秒为单位
import time
t1 = time.time()
t2=t1+10

print time.ctime(t1)#Wed Oct 26 21:15:30 2016
print time.ctime(t2)#Wed Oct 26 21:15:40 2016

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

datetime模块

datatime模块重新封装了time模块,提供更多接口,提供的类有:date,time,datetime,timedelta,tzinfo。
1、date类
datetime.date(year, month, day)
静态方法和字段

date.max、date.min:date对象所能表示的最大、最小日期;
date.resolution:date对象表示日期的最小单位。这里是天。
date.today():返回一个表示当前本地日期的date对象;
date.fromtimestamp(timestamp):根据给定的时间戮,返回一个date对象;

 
 
  • 1
  • 2
  • 3
  • 4

output

from datetime import *
import time

print ‘date.max:’, date.max
print ‘date.min:’, date.min
print ‘date.today():’, date.today()
print ‘date.fromtimestamp():’, date.fromtimestamp(time.time())

#Output======================
# date.max: 9999-12-31
# date.min: 0001-01-01
# date.today(): 2016-10-26
# date.fromtimestamp(): 2016-10-26

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

方法和属性

d1 = date(2011,06,03)#date对象
d1.year、date.month、date.day:年、月、日;
d1.replace(year, month, day):生成一个新的日期对象,用参数指定的年,月,日代替原有对象中的属性。(原有对象仍保持不变)
d1.timetuple():返回日期对应的time.struct_time对象;
d1.weekday():返回weekday,如果是星期一,返回0;如果是星期2,返回1,以此类推;
d1.isoweekday():返回weekday,如果是星期一,返回1;如果是星期2,返回2,以此类推;
d1.isocalendar():返回格式如(year,month,day)的元组;
d1.isoformat():返回格式如'YYYY-MM-DD’的字符串;
d1.strftime(fmt):和time模块format相同。

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

output

from datetime import *

now = date(2016, 10, 26)
tomorrow = now.replace(day = 27)
print ‘now:’, now, ‘, tomorrow:’, tomorrow
print ‘timetuple():’, now.timetuple()
print ‘weekday():’, now.weekday()
print ‘isoweekday():’, now.isoweekday()
print ‘isocalendar():’, now.isocalendar()
print ‘isoformat():’, now.isoformat()
print ‘strftime():’, now.strftime(“%Y-%m-%d”)

#Output========================
# now: 2016-10-26 , tomorrow: 2016-10-27
# timetuple(): time.struct_time(tm_year=2016, tm_mon=10, tm_mday=26, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=300, tm_isdst=-1)
# weekday(): 2
# isoweekday(): 3
# isocalendar(): (2016, 43, 3)
# isoformat(): 2016-10-26
# strftime(): 2016-10-26

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

2、time类
datetime.time(hour[ , minute[ , second[ , microsecond[ , tzinfo] ] ] ] )
静态方法和字段

time.min、time.max:time类所能表示的最小、最大时间。其中,time.min = time(0, 0, 0, 0), time.max = time(23, 59, 59, 999999);
time.resolution:时间的最小单位,这里是1微秒;

 
 
  • 1
  • 2

方法和属性

t1 = datetime.time(10,23,15)#time对象t1.hour、t1.minute、t1.second、t1.microsecond:时、分、秒、微秒;
t1.tzinfo:时区信息;
t1.replace([ hour[ , minute[ , second[ , microsecond[ , tzinfo] ] ] ] ] ):创建一个新的时间对象,用参数指定的时、分、秒、微秒代替原有对象中的属性(原有对象仍保持不变);
t1.isoformat():返回型如"HH:MM:SS"格式的字符串表示;
t1.strftime(fmt):同time模块中的format
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

output

from  datetime import *

tm = time(23, 46, 10)
print ‘tm:’, tm
print ‘hour: %d, minute: %d, second: %d, microsecond: %d’ % (tm.hour, tm.minute, tm.second, tm.microsecond)
tm1 = tm.replace(hour=20)
print ‘tm1:’, tm1
print ‘isoformat():’, tm.isoformat()
print ‘strftime()’, tm.strftime(“%X”)

#Output==============================================
# tm: 23:46:10
# hour: 23, minute: 46, second: 10, microsecond: 0
# tm1: 20:46:10
# isoformat(): 23:46:10
# strftime() 23:46:10

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

3、datetime类
datetime相当于date和time结合起来。datetime.datetime (year, month, day[ , hour[ , minute[ , second[ , microsecond[ , tzinfo] ] ] ] ] )
静态方法和字段

datetime.today():返回一个表示当前本地时间的datetime对象;
datetime.now([tz]):返回一个表示当前本地时间的datetime对象,如果提供了参数tz,则获取tz参数所指时区的本地时间;
datetime.utcnow():返回一个当前utc时间的datetime对象;#格林威治时间
datetime.fromtimestamp(timestamp[, tz]):根据时间戮创建一个datetime对象,参数tz指定时区信息;
datetime.utcfromtimestamp(timestamp):根据时间戮创建一个datetime对象;
datetime.combine(date, time):根据date和time,创建一个datetime对象;
datetime.strptime(date_string, format):将格式字符串转换为datetime对象;

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

output

from  datetime import *
import time

print ‘datetime.max:’, datetime.max
print ‘datetime.min:’, datetime.min
print ‘datetime.resolution:’, datetime.resolution
print ‘today():’, datetime.today()
print ‘now():’, datetime.now()
print ‘utcnow():’, datetime.utcnow()
print ‘fromtimestamp(tmstmp):’, datetime.fromtimestamp(time.time())
print ‘utcfromtimestamp(tmstmp):’, datetime.utcfromtimestamp(time.time())

#output======================
# datetime.max: 9999-12-31 23:59:59.999999
# datetime.min: 0001-01-01 00:00:00
# datetime.resolution: 0:00:00.000001
# today(): 2016-10-26 23:12:51.307000
# now(): 2016-10-26 23:12:51.307000
# utcnow(): 2016-10-26 15:12:51.307000
# fromtimestamp(tmstmp): 2016-10-26 23:12:51.307000
# utcfromtimestamp(tmstmp): 2016-10-26 15:12:51.307000

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

方法和属性

dt=datetime.now()#datetime对象
dt.year、month、day、hour、minute、second、microsecond、tzinfo:
dt.date():获取date对象;
dt.time():获取time对象;
dt. replace ([ year[ , month[ , day[ , hour[ , minute[ , second[ , microsecond[ , tzinfo] ] ] ] ] ] ] ]):
dt. timetuple ()
dt. utctimetuple ()
dt. toordinal ()
dt. weekday ()
dt. isocalendar ()
dt. isoformat ([ sep] )
dt. ctime ():返回一个日期时间的C格式字符串,等效于time.ctime(time.mktime(dt.timetuple()));
dt. strftime (format)

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

4.timedelta类,时间加减
使用timedelta可以很方便的在日期上做天days,小时hour,分钟,秒,毫秒,微妙的时间计算,如果要计算月份则需要另外的办法。

#coding:utf-8
from  datetime import *

dt = datetime.now()
#日期减一天
dt1 = dt + timedelta(days=-1)#昨天
dt2 = dt - timedelta(days=1)#昨天
dt3 = dt + timedelta(days=1)#明天
delta_obj = dt3-dt
print type(delta_obj),delta_obj#<type ‘datetime.timedelta’> 1 day, 0:00:00
print delta_obj.days ,delta_obj.total_seconds()#1 86400.0

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

** 5、tzinfo时区类**

#! /usr/bin/python
# coding=utf-8

from datetime import datetime, tzinfo,timedelta

“”"
tzinfo是关于时区信息的类
tzinfo是一个抽象类,所以不能直接被实例化
“”“

class UTC(tzinfo):
”““UTC””"
def init(self,offset = 0):
self._offset = offset

<span class="token keyword">def</span> <span class="token function">utcoffset</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> dt<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> timedelta<span class="token punctuation">(</span>hours<span class="token operator">=</span>self<span class="token punctuation">.</span>_offset<span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">tzname</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> dt<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> <span class="token string">"UTC +%s"</span> <span class="token operator">%</span> self<span class="token punctuation">.</span>_offset

<span class="token keyword">def</span> <span class="token function">dst</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> dt<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> timedelta<span class="token punctuation">(</span>hours<span class="token operator">=</span>self<span class="token punctuation">.</span>_offset<span class="token punctuation">)</span>

#北京时间
beijing = datetime(2011,11,11,0,0,0,tzinfo = UTC(8))
print “beijing time:”,beijing
#曼谷时间
bangkok = datetime(2011,11,11,0,0,0,tzinfo = UTC(7))
print “bangkok time”,bangkok
#北京时间转成曼谷时间
print “beijing-time to bangkok-time:”,beijing.astimezone(UTC(7))

#计算时间差时也会考虑时区的问题
timespan = beijing - bangkok
print “时差:”,timespan

#Output==================
# beijing time: 2011-11-11 00:00:00+08:00
# bangkok time 2011-11-11 00:00:00+07:00
# beijing-time to bangkok-time: 2011-11-10 23:00:00+07:00
# 时差: -1 day, 23:00:00

  • 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
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

random 模块

import random

 
 
  • 1
# 大于0且小于1之间的小数
print(random.random())

 
 
  • 1
  • 2
0.42866657593385415

 
 
  • 1
# 大于等于1且小于等于3之间的整数
print(random.randint(1, 3))

 
 
  • 1
  • 2
3

 
 
  • 1
# 大于等于1且小于3之间的整数
print(random.randrange(1, 3))

 
 
  • 1
  • 2
2

 
 
  • 1
# 大于1小于3的小数,如1.927109612082716
print(random.uniform(1, 3))

 
 
  • 1
  • 2
2.1789596280319605

 
 
  • 1
# 列表内的任意一个元素,即1或者‘23’或者[4,5]
print(random.choice([1, '23', [4, 5]]))

 
 
  • 1
  • 2
[4, 5]

 
 
  • 1
# random.sample([], n),列表元素任意n个元素的组合,示例n=2
print(random.sample([1, '23', [4, 5]], 2))

 
 
  • 1
  • 2
['23', 1]

 
 
  • 1
lis = [1, 3, 5, 7, 9]
# 打乱l的顺序,相当于"洗牌"
random.shuffle(lis)
print(lis)

 
 
  • 1
  • 2
  • 3
  • 4
[9, 1, 5, 7, 3]

 
 
  • 1

os 模块

os 模块负责程序与操作系统的交互,提供了访问操作系统底层的接口,多用于文件处理。

  • os.getcwd() 获取当前工作目录,即当前 python 脚本工作的目录路径
  • os.chdir(“dirname”) 改变当前脚本工作目录;相当于 shell 下 cd
  • os.curdir 返回当前目录: (‘.’)
  • os.pardir 获取当前目录的父目录字符串名:(‘…’)
  • os.makedirs(‘dirname1/dirname2’) 可生成多层递归目录
  • os.removedirs(‘dirname1’) 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
  • os.mkdir(‘dirname’) 生成单级目录;相当于 shell 中 mkdir dirname
  • os.rmdir(‘dirname’) 删除单级空目录,若目录不为空则无法删除,报错;相当于 shell 中 rmdir dirname
  • os.listdir(‘dirname’) 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
  • os.remove() 删除一个文件
  • os.rename(“oldname”,”newname”) 重命名文件/目录
  • os.stat(‘path/filename’) 获取文件/目录信息
  • os.sep 输出操作系统特定的路径分隔符,win 下为”",Linux 下为”/“
  • os.linesep 输出当前平台使用的行终止符,win 下为”\r\n”,Linux 下为”\n”
  • os.pathsep 输出用于分割文件路径的字符串 win 下为;,Linux 下为:
  • os.name 输出字符串指示当前使用平台。win->’nt’; Linux->’posix’
  • os.system(“bash command”) 运行 shell 命令,直接显示
  • os.environ 获取系统环境变量
  • os.path.abspath(path) 返回 path 规范化的绝对路径
  • os.path.split(path) 将 path 分割成目录和文件名二元组返回
  • os.path.dirname(path) 返回 path 的目录。其实就是 os.path.split(path)的第一个元素
  • os.path.basename(path) 返回 path 最后的文件名。如何 path 以/或\结尾,那么就会返回空值。即- os.path.split(path)的第二个元素
  • os.path.exists(path) 如果 path 存在,返回 True;如果 path 不存在,返回 False
  • os.path.isabs(path) 如果 path 是绝对路径,返回 True
  • os.path.isfile(path) 如果 path 是一个存在的文件,返回 True。否则返回 False
  • os.path.isdir(path)如果 path 是一个存在的目录,则返回 True。否则返回 False
  • os.path.join(path1[, path2[, …]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
  • os.path.getatime(path) 返回 path 所指向的文件或者目录的最后存取时间
  • os.path.getmtime(path) 返回 path 所指向的文件或者目录的最后修改时间
  • os.path.getsize(path) 返回 path 的大小
在Linux和Mac平台上,该函数会原样返回path,在windows平台上会将路径中所有字符转换为小写,并将所有斜杠转换为饭斜杠。
>>> os.path.normcase('c:/windows\\system32\\')
'c:\\windows\\system32\\'

规范化路径,如../
>>> os.path.normpath(‘c://windows\System32\…/Temp/’)
‘c:\windows\Temp’

>>> a=‘/Users/jieli/test1/\\a1/\\aa.py/…/…’
>>> print(os.path.normpath(a))
/Users/jieli/test1

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
os路径处理
#方式一:推荐使用
import os
#具体应用
import os,sys
possible_topdir = os.path.normpath(os.path.join(
    os.path.abspath(__file__),
    os.pardir, #上一级
    os.pardir,
    os.pardir
))
sys.path.insert(0,possible_topdir)

#方式二:不推荐使用
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(file))))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

sys 模块

sys 模块负责程序与 python 解释器的交互,提供了一系列的函数和变量,用于操控 python 的运行时环境。

  • sys.argv 命令行参数 List,第一个元素是程序本身路径
  • sys.modules.keys() 返回所有已经导入的模块列表
  • sys.exc_info() 获取当前正在处理的异常类,exc_type、exc_value、exc_traceback 当前处理的异常详细信息
  • sys.exit(n) 退出程序,正常退出时 exit(0)
  • sys.hexversion 获取 Python 解释程序的版本值,16 进制格式如:0x020403F0
  • sys.version 获取 Python 解释程序的版本信息
  • sys.maxint 最大的 Int 值
  • sys.maxunicode 最大的 Unicode 值
  • sys.modules 返回系统导入的模块字段,key 是模块名,value 是模块
  • sys.path 返回模块的搜索路径,初始化时使用 PYTHONPATH 环境变量的值
  • sys.platform 返回操作系统平台名称
  • sys.stdout 标准输出
  • sys.stdin 标准输入
  • sys.stderr 错误输出
  • sys.exc_clear() 用来清除当前线程所出现的当前的或最近的错误信息
  • sys.exec_prefix 返回平台独立的 python 文件安装的位置
  • sys.byteorder 本地字节规则的指示器,big-endian 平台的值是’big’,little-endian 平台的值是’little’
  • sys.copyright 记录 python 版权相关的东西
  • sys.api_version 解释器的 C 的 API 版本

json 和 pickle 模块

序列化
把对象(变量)从内存中变成可存储或传输的过程称之为序列化,在 Python 中叫 pickling,在其他语言中也被称之为 serialization,marshalling,flattening。
序列化的优点:

  1. 持久保存状态:内存是无法永久保存数据的,当程序运行了一段时间,我们断电或者重启程序,内存中关于这个程序的之前一段时间的数据(有结构)都被清空了。但是在断电或重启程序之前将程序当前内存中所有的数据都保存下来(保存到文件中),以便于下次程序执行能够从文件中载入之前的数据,然后继续执行,这就是序列化。
  2. 跨平台数据交互:序列化时不仅可以把序列化后的内容写入磁盘,还可以通过网络传输到别的机器上,如果收发的双方约定好实用一种序列化的格式,那么便打破了平台/语言差异化带来的限制,实现了跨平台数据交互。

json
Json 序列化并不是 python 独有的,json 序列化在 java 等语言中也会涉及到,因此使用 json 序列化能够达到跨平台传输数据的目的。
json 数据类型和 python 数据类型对应关系表

Json 类型Python 类型
{}dict
[]list
“string”str
520.13int 或 float
true/falseTrue/False
nullNone

json 模块序列化和反序列化的一个过程如下图所示

import json

struct_data = { ‘name’: ‘json’, ‘age’: 23, ‘sex’: ‘male’}
print(struct_data, type(struct_data))

{ ‘name’: ‘json’, ‘age’: 23, ‘sex’: ‘male’} <class ‘dict’>

data = json.dumps(struct_data)
print(data, type(data))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
{"name": "json", "age": 23, "sex": "male"} <class 'str'>

 
 
  • 1
# 注意:无论数据是怎样创建的,只要满足json格式(如果是字典,则字典内元素都是双引号),就可以json.loads出来,不一定非要dumps的数据才能loads
data = json.loads(data)
print(data, type(data))

{ ‘name’: ‘json’, ‘age’: 23, ‘sex’: ‘male’} <class ‘dict’>

  • 1
  • 2
  • 3
  • 4
  • 5
# 序列化
with open('Json序列化对象.json', 'w') as fw:
    json.dump(struct_data, fw)

 
 
  • 1
  • 2
  • 3
# 反序列化
with open('Json序列化对象.json') as fr:
    data = json.load(fr)
print(data)

{ ‘name’: ‘json’, ‘age’: 23, ‘sex’: ‘male’}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

pickle
Pickle 序列化和所有其他编程语言特有的序列化问题一样,它只能用于 Python,并且可能不同版本的 Python 彼此都不兼容,因此,只能用 Pickle 保存那些不重要的数据,即不能成功地反序列化也没关系。但是 pickle 的好处是可以存储 Python 中的所有的数据类型,包括对象,而 json 不可以。
pickle 模块序列化和反序列化的过程如下图所示
img

import pickle

 
 
  • 1
struct_data = {'name': 'json', 'age': 23, 'sex': 'male'}
print(struct_data, type(struct_data))

 
 
  • 1
  • 2
{'name': 'json', 'age': 23, 'sex': 'male'} <class 'dict'>

 
 
  • 1
data = pickle.dumps(struct_data)
print(data, type(data))

 
 
  • 1
  • 2
b'\x80\x03}q\x00(X\x04\x00\x00\x00nameq\x01X\x04\x00\x00\x00jsonq\x02X\x03\x00\x00\x00ageq\x03K\x17X\x03\x00\x00\x00sexq\x04X\x04\x00\x00\x00maleq\x05u.' <class 'bytes'>

 
 
  • 1
data = pickle.loads(data)
print(data, type(data))

 
 
  • 1
  • 2
{'name': 'json', 'age': 23, 'sex': 'male'} <class 'dict'>

 
 
  • 1
# 序列化(注意:pickle模块需要使用二进制存储,即'wb'模式存储)
with open('Pickle序列化对象.pkl', 'wb') as fw:
    pickle.dump(struct_data, fw)

 
 
  • 1
  • 2
  • 3
# 反序列化
with open('Pickle序列化对象.pkl', 'rb') as fr:
    pickle = pickle.load(fr)
print(data)

 
 
  • 1
  • 2
  • 3
  • 4
{'name': 'json', 'age': 23, 'sex': 'male'}

 
 
  • 1

hashlib和hmac 模块

hash 是什么
hash 是一种算法(Python3.版本里使用 hashlib 模块代替了 md5 模块和 sha 模块,主要提供 SHA1、SHA224、SHA256、SHA384、SHA512、MD5 算法),该算法接受传入的内容,经过运算得到一串 hash 值。
hash 值的特点:

  1. 只要传入的内容一样,得到的 hash 值一样,可用于非明文密码传输时密码校验
  2. 不能由 hash 值返解成内容,即可以保证非明文密码的安全性
  3. 只要使用的 hash 算法不变,无论校验的内容有多大,得到的 hash 值长度是固定的,可以用于对文本的哈希处理

hash 算法其实可以看成如下图所示的一座工厂,工厂接收你送来的原材料,经过加工返回的产品就是 hash 值
img

import hashlib

m = hashlib.md5()

m.update(‘hello’.encode(‘utf8’))
print(m.hexdigest())

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
5d41402abc4b2a76b9719d911017c592

 
 
  • 1
m.update('hash'.encode('utf8'))
print(m.hexdigest())

 
 
  • 1
  • 2
97fa850988687b8ceb12d773347f7712

 
 
  • 1
m2 = hashlib.md5()
m2.update('hellohash'.encode('utf8'))
print(m2.hexdigest())

 
 
  • 1
  • 2
  • 3
97fa850988687b8ceb12d773347f7712

 
 
  • 1
m3 = hashlib.md5()
m3.update('hello'.encode('utf8'))
print(m3.hexdigest())

 
 
  • 1
  • 2
  • 3
5d41402abc4b2a76b9719d911017c592

 
 
  • 1

撞库破解 hash 算法加密
hash 加密算法虽然看起来很厉害,但是他是存在一定缺陷的,即可以通过撞库可以反解,如下代码所示。

import hashlib

# 假定我们知道hash的微信会设置如下几个密码
pwd_list = [
‘hash3714’,
‘hash1313’,
‘hash94139413’,
‘hash123456’,
‘123456hash’,
‘h123ash’,
]

def make_pwd_dic(pwd_list):
dic = { }
for pwd in pwd_list:
m = hashlib.md5()
m.update(pwd.encode(‘utf-8’))
dic[pwd] = m.hexdigest()
return dic

def break_code(hash_pwd, pwd_dic):
for k, v in pwd_dic.items():
if v hash_pwd:
print('hash的微信的密码是=>%s’ % k)

hash_pwd = ‘0562b36c3c5a3925dbe3c4d32a4f2ba2’
break_code(hash_pwd, make_pwd_dic(pwd_list))

  • 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
hash的微信的密码是===>hash123456

 
 
  • 1

hmac 模块
为了防止密码被撞库,我们可以使用 python 中的另一个 hmac 模块,它内部对我们创建 key 和内容做过某种处理后再加密。
如果要保证 hmac 模块最终结果一致,必须保证:

  1. hmac.new 括号内指定的初始 key 一样
  2. 无论 update 多少次,校验的内容累加到一起是一样的内容
import hmac

# 注意hmac模块只接受二进制数据的加密
h1 = hmac.new(b’hash’)
h1.update(b’hello’)
h1.update(b’world’)
print(h1.hexdigest())

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
905f549c5722b5850d602862c34a763e

 
 
  • 1
h2 = hmac.new(b'hash')
h2.update(b'helloworld')
print(h2.hexdigest())

 
 
  • 1
  • 2
  • 3
905f549c5722b5850d602862c34a763e

 
 
  • 1
h3 = hmac.new(b'hashhelloworld')
print(h3.hexdigest())

 
 
  • 1
  • 2
a7e524ade8ac5f7f33f3a39a8f63fd25

 
 
  • 1

logging 模块

低配 logging

日志总共分为以下五个级别,这个五个级别自下而上进行匹配 debug–>info–>warning–>error–>critical,默认最低级别为 warning 级别。
V1版本

import logging

logging.debug(‘调试信息’)
logging.info(‘正常信息’)
logging.warning(‘警告信息’)
logging.error(‘报错信息’)
logging.critical(‘严重错误信息’)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
WARNING:root:警告信息
ERROR:root:报错信息
CRITICAL:root:严重错误信息

 
 
  • 1
  • 2
  • 3

v1 版本无法指定日志的级别;无法指定日志的格式;只能往屏幕打印,无法写入文件。因此可以改成下述的代码。

v2版本

import logging

# 日志的基本配置

logging.basicConfig(filename=‘access.log’,
format=‘%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s’,
datefmt=‘%Y-%m-%d %H:%M:%S %p’,
level=10)

logging.debug(‘调试信息’) # 10
logging.info(‘正常信息’) # 20
logging.warning(‘警告信息’) # 30
logging.error(‘报错信息’) # 40
logging.critical(‘严重错误信息’) # 50

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
可在logging.basicConfig()函数中可通过具体参数来更改logging模块默认行为,可用参数有:

filename:用指定的文件名创建FiledHandler(后边会具体讲解handler的概念),这样日志会被存储在指定的文件中。
filemode:文件打开方式,在指定了filename时使用这个参数,默认值为“a”还可指定为“w”。
format:指定handler使用的日志显示格式。
datefmt:指定日期时间格式。
level:设置rootlogger(后边会讲解具体概念)的日志级别
stream:用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件,默认为sys.stderr。若同时列出了filename和stream两个参数,则stream参数会被忽略。

format参数中可能用到的格式化串:

%(name)s Logger的名字
%(levelno)s 数字形式的日志级别
%(levelname)s 文本形式的日志级别
%(pathname)s 调用日志输出函数的模块的完整路径名,可能没有
%(filename)s 调用日志输出函数的模块的文件名
%(module)s 调用日志输出函数的模块名
%(funcName)s 调用日志输出函数的函数名
%(lineno)d 调用日志输出函数的语句所在的代码行
%(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示
%(relativeCreated)d 输出日志信息时的,自Logger创建以 来的毫秒数
%(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
%(thread)d 线程ID。可能没有
%(threadName)s 线程名。可能没有
%(process)d 进程ID。可能没有
%(message)s用户输出的消息

  • 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

v2 版本不能指定字符编码;只能往文件中打印。
v3版本
logging 模块包含四种角色:logger、Filter、Formatter 对象、Handler

  1. logger:产生日志的对象
  2. Filter:过滤日志的对象
  3. Formatter 对象:可以定制不同的日志格式对象,然后绑定给不同的 Handler 对象使用,以此来控制不同的 Handler 的日志格式
  4. Handler:接收日志然后控制打印到不同的地方,FileHandler 用来打印到文件中,StreamHandler 用来打印到终端
'''
critical=50
error =40
warning =30
info = 20
debug =10
'''

import logging

# 1、logger对象:负责产生日志,然后交给Filter过滤,然后交给不同的Handler输出
logger = logging.getLogger(file)

# 2、Filter对象:不常用,略

# 3、Handler对象:接收logger传来的日志,然后控制输出
h1 = logging.FileHandler(‘t1.log’) # 打印到文件
h2 = logging.FileHandler(‘t2.log’) # 打印到文件
sm = logging.StreamHandler() # 打印到终端

# 4、Formatter对象:日志格式
formmater1 = logging.Formatter(‘%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s’,
datefmt=‘%Y-%m-%d %H:%M:%S %p’,)

formmater2 = logging.Formatter(‘%(asctime)s : %(message)s’,
datefmt=‘%Y-%m-%d %H:%M:%S %p’,)

formmater3 = logging.Formatter(‘%(name)s %(message)s’,)

# 5、为Handler对象绑定格式
h1.setFormatter(formmater1)
h2.setFormatter(formmater2)
sm.setFormatter(formmater3)

# 6、将Handler添加给logger并设置日志级别
logger.addHandler(h1)
logger.addHandler(h2)
logger.addHandler(sm)

# 设置日志级别,可以在两个关卡进行设置logger与handler
# logger是第一级过滤,然后才能到handler
logger.setLevel(30)
h1.setLevel(10)
h2.setLevel(10)
sm.setLevel(10)

# 7、测试
logger.debug(‘debug’)
logger.info(‘info’)
logger.warning(‘warning’)
logger.error(‘error’)
logger.critical(‘critical’)

  • 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
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

高配 logging

配置日志文件
以上三个版本的日志只是为了引出我们下面的日志配置文件

import os
import logging.config

# 定义三种日志输出格式 开始
standard_format = ‘[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]’
‘[%(levelname)s][%(message)s]’ # 其中name为getLogger()指定的名字;lineno为调用日志输出函数的语句所在的代码行
simple_format = ‘[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s’
id_simple_format = ‘[%(levelname)s][%(asctime)s] %(message)s’
# 定义日志输出格式 结束

logfile_dir = os.path.dirname(os.path.dirname(os.path.abspath(file))) # log文件的目录,需要自定义文件路径 # atm
logfile_dir = os.path.join(logfile_dir, ‘log’) # C:\Users\oldboy\Desktop\atm\log

logfile_name = ‘log.log’ # log文件名,需要自定义路径名

# 如果不存在定义的日志目录就创建一个
if not os.path.isdir(logfile_dir): # C:\Users\oldboy\Desktop\atm\log
os.mkdir(logfile_dir)

# log文件的全路径
logfile_path = os.path.join(logfile_dir, logfile_name) # C:\Users\oldboy\Desktop\atm\log\log.log
# 定义日志路径 结束

# log配置字典
LOGGING_DIC = {
‘version’: 1,
‘disable_existing_loggers’: False,
‘formatters’: {
‘standard’: {
‘format’: standard_format
},
‘simple’: {
‘format’: simple_format
},
},
‘filters’: { }, # filter可以不定义
‘handlers’: {
# 打印到终端的日志
‘console’: {
‘level’: ‘DEBUG’,
‘class’: ‘logging.StreamHandler’, # 打印到屏幕
‘formatter’: ‘simple’
},
# 打印到文件的日志,收集info及以上的日志
‘default’: {
‘level’: ‘INFO’,
‘class’: ‘logging.handlers.RotatingFileHandler’, # 保存到文件
‘formatter’: ‘standard’,
‘filename’: logfile_path, # 日志文件
‘maxBytes’: 1024 1024 5, # 日志大小 5M (*****)
‘backupCount’: 5,
‘encoding’: ‘utf-8’, # 日志文件的编码,再也不用担心中文log乱码了
},
},
‘loggers’: {
# logging.getLogger(name)拿到的logger配置。如果’‘设置为固定值logger1,则下次导入必须设置成logging.getLogger(‘logger1’)
’': {
# 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
‘handlers’: [‘default’, ‘console’],
‘level’: ‘DEBUG’,
‘propagate’: False, # 向上(更高level的logger)传递
},
},
}

def load_my_logging_cfg():
logging.config.dictConfig(LOGGING_DIC) # 导入上面定义的logging配置
logger = logging.getLogger(name) # 生成一个log实例
logger.info(‘It works!’) # 记录该文件的运行状态

<span class="token keyword">return</span> logger

if name == main:
load_my_logging_cfg()

  • 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
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77

使用日志

import time
import logging
import my_logging  # 导入自定义的logging配置

logger = logging.getLogger(name) # 生成logger实例

def demo():
logger.debug(“start range… time:{}”.format(time.time()))
logger.info(“中文测试开始。。。”)
for i in range(10):
logger.debug(“i:{}”.format(i))
time.sleep(0.2)
else:
logger.debug(“over range… time:{}”.format(time.time()))
logger.info(“中文测试结束。。。”)

if name == main:
my_logging.load_my_logging_cfg() # 在你程序文件的入口加载自定义logging配置
demo()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

Django 日志配置文件

# logging_config.py

LOGGING = {
‘version’: 1,
‘disable_existing_loggers’: False,
‘formatters’: {
‘standard’: {
‘format’: ‘[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]’
‘[%(levelname)s][%(message)s]’
},
‘simple’: {
‘format’: ‘[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s’
},
‘collect’: {
‘format’: ‘%(message)s’
}
},
‘filters’: {
‘require_debug_true’: {
‘()’: ‘django.utils.log.RequireDebugTrue’,
},
},
‘handlers’: {
# 打印到终端的日志
‘console’: {
‘level’: ‘DEBUG’,
‘filters’: [‘require_debug_true’],
‘class’: ‘logging.StreamHandler’,
‘formatter’: ‘simple’
},
# 打印到文件的日志,收集info及以上的日志
‘default’: {
‘level’: ‘INFO’,
‘class’: ‘logging.handlers.RotatingFileHandler’, # 保存到文件,自动切
‘filename’: os.path.join(BASE_LOG_DIR, “xxx_info.log”), # 日志文件
‘maxBytes’: 1024 1024 5, # 日志大小 5M
‘backupCount’: 3,
‘formatter’: ‘standard’,
‘encoding’: ‘utf-8’,
},
# 打印到文件的日志:收集错误及以上的日志
‘error’: {
‘level’: ‘ERROR’,
‘class’: ‘logging.handlers.RotatingFileHandler’, # 保存到文件,自动切
‘filename’: os.path.join(BASE_LOG_DIR, “xxx_err.log”), # 日志文件
‘maxBytes’: 1024 1024 5, # 日志大小 5M
‘backupCount’: 5,
‘formatter’: ‘standard’,
‘encoding’: ‘utf-8’,
},
# 打印到文件的日志
‘collect’: {
‘level’: ‘INFO’,
‘class’: ‘logging.handlers.RotatingFileHandler’, # 保存到文件,自动切
‘filename’: os.path.join(BASE_LOG_DIR, “xxx_collect.log”),
‘maxBytes’: 1024 1024 5, # 日志大小 5M
‘backupCount’: 5,
‘formatter’: ‘collect’,
‘encoding’: “utf-8”
}
},
‘loggers’: {
# logging.getLogger(name)拿到的logger配置
‘’: {
‘handlers’: [‘default’, ‘console’, ‘error’],
‘level’: ‘DEBUG’,
‘propagate’: True,
},
# logging.getLogger(‘collect’)拿到的logger配置
‘collect’: {
‘handlers’: [‘console’, ‘collect’],
‘level’: ‘INFO’,
}
},
}

# -----------
# 用法:拿到俩个logger

logger = logging.getLogger(name) # 线上正常的日志
collect_logger = logging.getLogger(“collect”) # 领导说,需要为领导们单独定制领导们看的日志

  • 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
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82

numpy 模块

numpy 官方文档:https://docs.scipy.org/doc/numpy/reference/?v=20190307135750
numpy 是 Python 的一种开源的数值计算扩展库。这种库可用来存储和处理大型 numpy 数组,比 Python 自身的嵌套列表结构要高效的多(该结构也可以用来表示 numpy 数组)。
numpy 库有两个作用:

  1. 区别于 list 列表,提供了数组操作、数组运算、以及统计分布和简单的数学模型
  2. 计算速度快,甚至要由于 python 内置的简单运算,使得其成为 pandas、sklearn 等模块的依赖包。高级的框架如 TensorFlow、PyTorch 等,其数组操作也和 numpy 非常相似。

为什么用 numpy

lis1 = [1, 2, 3]
lis2 = [4, 5, 6]

 
 
  • 1
  • 2

如果我们想让lis1 * lis2得到一个结果为lis_res = [4, 10, 18],非常复杂。

创建 numpy 数组

numpy 数组即 numpy 的 ndarray 对象,创建 numpy 数组就是把一个列表传入 np.array()方法。

import numpy as np

 
 
  • 1
# np.array? 相当于pycharm的ctrl+鼠标左键

 
 
  • 1
# 创建一维的ndarray对象
arr = np.array([1, 2, 3])
print(arr, type(arr))

 
 
  • 1
  • 2
  • 3
[1 2 3] <class 'numpy.ndarray'>

 
 
  • 1
# 创建二维的ndarray对象
print(np.array([[1, 2, 3], [4, 5, 6]]))

 
 
  • 1
  • 2
[[1 2 3]
 [4 5 6]]

 
 
  • 1
  • 2
# 创建三维的ndarray对象
print(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))

 
 
  • 1
  • 2
[[1 2 3]
 [4 5 6]
 [7 8 9]]

 
 
  • 1
  • 2
  • 3

numpy 数组的常用属性

属性解释
T数组的转置(对高维数组而言)
dtype数组元素的数据类型
size数组元素的个数
ndim数组的维数
shape数组的维度大小(以元组形式)
astype类型转换

dtype 种类:bool_, int(8,16,32,64), float(16,32,64)

arr = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)
print(arr)

 
 
  • 1
  • 2
[[1. 2. 3.]
 [4. 5. 6.]]

 
 
  • 1
  • 2
print(arr.T)

 
 
  • 1
[[1. 4.]
 [2. 5.]
 [3. 6.]]

 
 
  • 1
  • 2
  • 3
print(arr.dtype)

 
 
  • 1
float32

 
 
  • 1
arr = arr.astype(np.int32)
print(arr.dtype)
print(arr)

 
 
  • 1
  • 2
  • 3
int32
[[1 2 3]
 [4 5 6]]

 
 
  • 1
  • 2
  • 3
print(arr.size)

 
 
  • 1
6

 
 
  • 1
print(arr.ndim)

 
 
  • 1
2

 
 
  • 1
print(arr.shape)

 
 
  • 1
(2, 3)

 
 
  • 1

获取 numpy 数组的行列数

由于 numpy 数组是多维的,对于二维的数组而言,numpy 数组就是既有行又有列。
注意:对于 numpy 我们一般多讨论二维的数组。

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)

 
 
  • 1
  • 2
[[1 2 3]
 [4 5 6]]

 
 
  • 1
  • 2
# 获取numpy数组的行和列构成的数组
print(arr.shape)

 
 
  • 1
  • 2
(2, 3)

 
 
  • 1
# 获取numpy数组的行
print(arr.shape[0])

 
 
  • 1
  • 2
2

 
 
  • 1
# 获取numpy数组的列
print(arr.shape[1])

 
 
  • 1
  • 2
3

 
 
  • 1

切割 numpy 数组

切分 numpy 数组类似于列表的切割,但是与列表的切割不同的是,numpy 数组的切割涉及到行和列的切割,但是两者切割的方式都是从索引 0 开始,并且取头不取尾。

arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(arr)

 
 
  • 1
  • 2
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]

 
 
  • 1
  • 2
  • 3
# 取所有元素
print(arr[:, :])

 
 
  • 1
  • 2
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]

 
 
  • 1
  • 2
  • 3
# 取第一行的所有元素
print(arr[:1, :])

 
 
  • 1
  • 2
[[1 2 3 4]]

 
 
  • 1
# 取第一行的所有元素
print(arr[0, [0, 1, 2, 3]])

 
 
  • 1
  • 2
[1 2 3 4]

 
 
  • 1
# 取第一列的所有元素
print(arr[:, :1])

 
 
  • 1
  • 2
[[1]
 [5]
 [9]]

 
 
  • 1
  • 2
  • 3
# 取第一列的所有元素
print(arr[(0, 1, 2), 0])

 
 
  • 1
  • 2
[1 5 9]

 
 
  • 1
# 取第一行第一列的元素
print(arr[(0, 1, 2), 0])

 
 
  • 1
  • 2
[1 5 9]

 
 
  • 1
# 取第一行第一列的元素
print(arr[0, 0])

 
 
  • 1
  • 2
1

 
 
  • 1
# 取大于5的元素,返回一个数组
print(arr[arr > 5])

 
 
  • 1
  • 2
[ 6  7  8  9 10 11 12]

 
 
  • 1
# numpy数组按运算符取元素的原理,即通过arr > 5生成一个布尔numpy数组
print(arr > 5)

 
 
  • 1
  • 2
[[False False False False]
 [False  True  True  True]
 [ True  True  True  True]]

 
 
  • 1
  • 2
  • 3

numpy 数组元素替换  
numpy 数组元素的替换,类似于列表元素的替换,并且 numpy 数组也是一个可变类型的数据,即如果对 numpy 数组进行替换操作,会修改原 numpy 数组的元素,所以下面我们用.copy()方法举例 numpy 数组元素的替换。

arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(arr)

 
 
  • 1
  • 2
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]

 
 
  • 1
  • 2
  • 3
# 取第一行的所有元素,并且让第一行的元素都为0
arr1 = arr.copy()
arr1[:1, :] = 0
print(arr1)

 
 
  • 1
  • 2
  • 3
  • 4
[[ 0  0  0  0]
 [ 5  6  7  8]
 [ 9 10 11 12]]

 
 
  • 1
  • 2
  • 3
# 取所有大于5的元素,并且让大于5的元素为0
arr2 = arr.copy()
arr2[arr > 5] = 0
print(arr2)

 
 
  • 1
  • 2
  • 3
  • 4
[[1 2 3 4]
 [5 0 0 0]
 [0 0 0 0]]

 
 
  • 1
  • 2
  • 3
# 对numpy数组清零
arr3 = arr.copy()
arr3[:, :] = 0
print(arr3)

 
 
  • 1
  • 2
  • 3
  • 4
[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]

 
 
  • 1
  • 2
  • 3

numpy 数组的合并

arr1 = np.array([[1, 2], [3, 4], [5, 6]])
print(arr1)

 
 
  • 1
  • 2
[[1 2]
 [3 4]
 [5 6]]

 
 
  • 1
  • 2
  • 3
arr2 = np.array([[7, 8], [9, 10], [11, 12]])
print(arr2)

 
 
  • 1
  • 2
[[ 7  8]
 [ 9 10]
 [11 12]]

 
 
  • 1
  • 2
  • 3
# 合并两个numpy数组的行,注意使用hstack()方法合并numpy数组,numpy数组应该有相同的行,其中hstack的h表示horizontal水平的
print(np.hstack((arr1, arr2)))

 
 
  • 1
  • 2
[[ 1  2  7  8]
 [ 3  4  9 10]
 [ 5  6 11 12]]

 
 
  • 1
  • 2
  • 3
# 合并两个numpy数组,其中axis=1表示合并两个numpy数组的行
print(np.concatenate((arr1, arr2), axis=1))

 
 
  • 1
  • 2
[[ 1  2  7  8]
 [ 3  4  9 10]
 [ 5  6 11 12]]

 
 
  • 1
  • 2
  • 3
# 合并两个numpy数组的列,注意使用vstack()方法合并numpy数组,numpy数组应该有相同的列,其中vstack的v表示vertical垂直的
print(np.vstack((arr1, arr2)))

 
 
  • 1
  • 2
[[ 1  2]
 [ 3  4]
 [ 5  6]
 [ 7  8]
 [ 9 10]
 [11 12]]

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
# 合并两个numpy数组,其中axis=0表示合并两个numpy数组的列
print(np.concatenate((arr1, arr2), axis=0))

 
 
  • 1
  • 2
[[ 1  2]
 [ 3  4]
 [ 5  6]
 [ 7  8]
 [ 9 10]
 [11 12]]

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

通过函数创建 numpy 数组

  • arange() range 的 numpy 版,支持浮点数

  • linspace() 类似 arange(),第三个参数为数组长度

  • zeros() 根据指定形状和 dtype 创建全 0 数组

  • ones() 根据指定形状和 dtype 创建全 1 数组

  • eye() 创建单位矩阵

  • empty() 创建一个元素全随机的数组

  • reshape() 重塑形状

array

arr = np.array([1, 2, 3])
print(arr)

 
 
  • 1
  • 2
[1 2 3]

 
 
  • 1

arange

# 构造0-9的ndarray数组
print(np.arange(10))

 
 
  • 1
  • 2
[0 1 2 3 4 5 6 7 8 9]

 
 
  • 1
# 构造1-4的ndarray数组
print(np.arange(1, 5))

 
 
  • 1
  • 2
[1 2 3 4]

 
 
  • 1
# 构造1-19且步长为2的ndarray数组
print(np.arange(1, 20, 2))

 
 
  • 1
  • 2
[ 1  3  5  7  9 11 13 15 17 19]

 
 
  • 1

linspace/logspace

# 构造一个等差数列,取头也取尾,从0取到20,取5个数
print(np.linspace(0, 20, 5))

 
 
  • 1
  • 2
[ 0.  5. 10. 15. 20.]

 
 
  • 1
# 构造一个等比数列,从10**0取到10**20,取5个数
print(np.logspace(0, 20, 5))

 
 
  • 1
  • 2
[1.e+00 1.e+05 1.e+10 1.e+15 1.e+20]

 
 
  • 1

zeros/ones/eye/empty

# 构造3*4的全0numpy数组
print(np.zeros((3, 4)))

 
 
  • 1
  • 2
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]

 
 
  • 1
  • 2
  • 3
# 构造3*4的全1numpy数组
print(np.ones((3, 4)))

 
 
  • 1
  • 2
[[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]

 
 
  • 1
  • 2
  • 3
# 构造3个主元的单位numpy数组
print(np.eye(3))

 
 
  • 1
  • 2
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

 
 
  • 1
  • 2
  • 3
# 构造一个4*4的随机numpy数组,里面的元素是随机生成的
print(np.empty((4, 4)))

 
 
  • 1
  • 2
[[ 2.31584178e+077 -1.49457545e-154  3.95252517e-323  0.00000000e+000]
 [ 0.00000000e+000  0.00000000e+000  0.00000000e+000  0.00000000e+000]
 [ 0.00000000e+000  0.00000000e+000  0.00000000e+000  0.00000000e+000]
 [ 0.00000000e+000  0.00000000e+000  1.29074055e-231  1.11687366e-308]]

 
 
  • 1
  • 2
  • 3
  • 4

reshape

arr = np.ones([2, 2], dtype=int)
print(arr.reshape(4, 1))

 
 
  • 1
  • 2
[[1]
 [1]
 [1]
 [1]]

 
 
  • 1
  • 2
  • 3
  • 4

fromstring/fromfunction(了解)

# fromstring通过对字符串的字符编码所对应ASCII编码的位置,生成一个ndarray对象
s = 'abcdef'
# np.int8表示一个字符的字节数为8
print(np.fromstring(s, dtype=np.int8))

 
 
  • 1
  • 2
  • 3
  • 4
[ 97  98  99 100 101 102]

/Applications/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:4: DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead
after removing the cwd from sys.path.

  • 1
  • 2
  • 3
  • 4
  • 5
def func(i, j):
    """其中i为numpy数组的行,j为numpy数组的列"""
    return i * j

# 使用函数对numpy数组元素的行和列的索引做处理,得到当前元素的值,索引从0开始,并构造一个3*4的numpy数组
print(np.fromfunction(func, (3, 4)))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
[[0. 0. 0. 0.]
 [0. 1. 2. 3.]
 [0. 2. 4. 6.]]

 
 
  • 1
  • 2
  • 3

numpy 数组运算

  • + 两个 numpy 数组对应元素相加
  • - 两个 numpy 数组对应元素相减
  • *两个 numpy 数组对应元素相乘

  • / 两个 numpy 数组对应元素相除,如果都是整数则取商

  • % 两个 numpy 数组对应元素相除后取余数

  • **n 单个 numpy 数组每个元素都取 n 次方,如**2:每个元素都取平方

arrarr1 = np.array([[1, 2], [3, 4], [5
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值