这一部分是python内置模块系列的最后一部分,介绍了一些小巧有用的内置模块。

目录:

1.random

2.shelve

3.getpass

4.zipfile

5.tarfile

6.bisect

7.fileinput

一、random

random模块是python提供的生成伪随机数模块,使用很简单,如下的三个方法都是生成1个随机数:

import random

print(random.random())
print(random.randint(1, 2))
print(random.randrange(1, 10))

还有一些方法比较有意思,比如seed和choice。

import random

alist = [1, 2, 3, 4, 5, 6, 7, 8, 9]
t = random.choice(alist)
print(t)

运行结果:

程序会在alist里面随机选择一个数。

下面是生成一个包含大写字母A-Z和数字0-9的随机验证码的程序

import random

checkcode = ''
for i in range(4):
    current = random.randrange(0,4)
    if current != i:
        temp = chr(random.randint(65,90))
    else:
        temp = random.randint(0,9)
    checkcode += str(temp)
print(checkcode)

二、shelve

shelve模块是一个简单的k,v键值对,将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式。

它只有一个open和一个close方法,与python的内置文件处理方法很像。

import shelve

d = shelve.open('shelve_test') #打开一个文件

class Test(object):
    def __init__(self,n):
        self.n = n

t = Test(123)
t2 = Test(123334)

name = ["alex","rain","test"]
d["test"] = name #持久化列表
d["t1"] = t      #持久化类
d["t2"] = t2

d.close()

唯一需要注意的是,通过open获得的内容读入内存后,所进行的修改在close之前不会保存到文件中,因此看下面的代码:

import shelve

s = shelve.open("testfile")
s["key"] = ['a', 'b', 'c']
print(s['key'])
s['key'].append('d')
print(s['key'])

运行结果:

['a', 'b', 'c']
['a', 'b', 'c']

“d”并没有被添加到列表的里面。怎么解决这个问题?使用一个临时变量!

import shelve

s = shelve.open("testfile")
s["key"] = ['a', 'b', 'c']
print(s["key"])
tmp = s["key"]
tmp.append('d')
s["key"] = tmp
print(s["key"])

运行结果:

['a', 'b', 'c']
['a', 'b', 'c', 'd']

三、getpass

  getpass模块非常简单,它能够让你在输入密码的时候不会在屏幕上显示密码,安全性更高。注意:在pycharm环境里这个模块用不了!

  getpass模块只有2个常用方法:getpass和getuser。参考下面的例子:

import getpass

pwd = getpass.getpass("请输入密码: ")  # 可代替input方法,接收用户输入的密码

print(getpass.getuser())  # getuser方法会返回当前执行程序的用户名

四、zipfile

  当你需要压缩文件的时候,使用这个模块会比较方便。

import zipfile

# 压缩
z = zipfile.ZipFile('laxi.zip', 'w')
z.write('a.log')
z.write('data.data')             # 可以一个一个添加文件进压缩包
z.close()

# 解压
z = zipfile.ZipFile('laxi.zip', 'r')
z.extractall()                              # 这是一次性将压缩包内的文件全部解压出来
z.close()

  要单独解压压缩包内的某个文件就需要先获得压缩包内的文件名列表。zipfile提供了一个namelist方法。

import zipfile

z = zipfile.ZipFile('laxi.zip', 'r')
ret = z.namelist()
print(ret)
z.close()

运行结果:

['testfile.bak', 'testfile.dat']

  然后通过具体的文件名去解压某个文件。zipfile提供了extract方法。

import zipfile

z = zipfile.ZipFile('laxi.zip', 'r')
z.extract("testfile.bak")
z.close()

五、tarfile

  当你需要对文件进行打包的时候,这个模块比较方便。一般的用法如下:

import tarfile

# 压缩
tar = tarfile.open('your.tar','w')
tar.add('/Users/wupeiqi/PycharmProjects/bbs2.log', arcname='bbs2.log')
tar.add('/Users/wupeiqi/PycharmProjects/cmdb.log', arcname='cmdb.log')
tar.close()

# 解压
tar = tarfile.open('your.tar','r')
tar.extractall()  # 可设置解压地址
tar.close()

tarfile解压缩

注意和zipfile的区别,在实例对象的时候用的是open方法,而不是类似“zipfile”的方法。其它用法基本类似。

如果要解包单独的文件,请先用getmembers方法获取包内的文件名,然后使用extract方法解包指定的文件,类似上面的zipfile模块。

六、bisect

  这是一个python内置的二分查找法模块,其源码短得可怜。

"""Bisection algorithms."""

def insort_right(a, x, lo=0, hi=None):
    """Insert item x in list a, and keep it sorted assuming a is sorted.

    If x is already in a, insert it to the right of the rightmost x.

    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.
    """

    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if x < a[mid]: hi = mid
        else: lo = mid+1
    a.insert(lo, x)

insort = insort_right   # backward compatibility

def bisect_right(a, x, lo=0, hi=None):
    """Return the index where to insert item x in list a, assuming a is sorted.

    The return value i is such that all e in a[:i] have e <= x, and all e in
    a[i:] have e > x.  So if x already appears in the list, a.insert(x) will
    insert just after the rightmost x already there.

    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.
    """

    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if x < a[mid]: hi = mid
        else: lo = mid+1
    return lo

bisect = bisect_right   # backward compatibility

def insort_left(a, x, lo=0, hi=None):
    """Insert item x in list a, and keep it sorted assuming a is sorted.

    If x is already in a, insert it to the left of the leftmost x.

    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.
    """

    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if a[mid] < x: lo = mid+1
        else: hi = mid
    a.insert(lo, x)

def bisect_left(a, x, lo=0, hi=None):
    """Return the index where to insert item x in list a, assuming a is sorted.

    The return value i is such that all e in a[:i] have e < x, and all e in
    a[i:] have e >= x.  So if x already appears in the list, a.insert(x) will
    insert just before the leftmost x already there.

    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.
    """

    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if a[mid] < x: lo = mid+1
        else: hi = mid
    return lo

# Overwrite above definitions with a fast C implementation
try:
    from _bisect import *
except ImportError:
    pass

bisect源码

  模块内只有4个方法:bisect_right、bisect_left、insort_right和insort_left。然后通过将bisect_right赋值给bisect,将insort_right赋值给insort实现向后兼容。实际使用中,是需要记住bisect和insort两个方法即可。

  bisect模块的使用方法通常是:bisect.bisect(list,x),其中x表示要查找的值,list是一个默认已经排好序的列表。

  bisect():返回值是x在列表中应处的位置下标,并不修改列表本身。

import bisect

x = 200
list1 = [1, 3, 6, 24, 55, 78, 454, 555, 1234, 6900]
ret = bisect.bisect(list1, x)
print("返回值: ", ret)
print("list1 = ", list1)

运行结果:

返回值:  6
list1 =  [1, 3, 6, 24, 55, 78, 454, 555, 1234, 6900]

  insort():返回值是None,但是会将x插入到列表中生成新的列表。x插在它的大小排序所在位置。

import bisect

x = 200
list1 = [1, 3, 6, 24, 55, 78, 454, 555, 1234, 6900]
ret = bisect.insort(list1, x)
print("返回值: ", ret)
print("list1 = ", list1)

运行结果:

返回值:  None
list1 =  [1, 3, 6, 24, 55, 78, 200, 454, 555, 1234, 6900]

七、fileinput

  使用过python内置的open方法处理文件的同学都知道,它没有直接修改文件内容的方法。这个fileinput可以帮助我们轻松的实现文件内容的修改功能。fileinput模块中的重要方法如下:

input([files[, inplace[, backup]])

  最重要的方法,用于遍历打开的文件,inplace为True的时候,会将修改写入文件,backup为True的时候会进行备份操作。

filename()

  返回当前文件的名字

lineno()

  返回当前(累计)的行数。同时处理多个文件时,行数会累计。

filelineno()

  返回当前文件的总行数。处理新文件时,行数会重置为1,重新计数。

isfirstline()

  检查当前行是否是文件的第一行

isstdin()

  检查最后一行是否来自sys.stdin

nextfile()

  关闭当前文件,移动到下一个文件(fileinput是可以同时处理多个文件的!)

close() 

  关闭整个文件链,结束迭代。

  为了演示fileinput的使用,假设编写了如下的一个脚本,想要为其代码进行编号。为了让脚本在进行代码编号后仍然能够正常运行,我们只能在每一行的右侧加上#来注释行号。其中,我们假定每个代码行最多有40个字符。具体代码如下:

import fileinput                        

f = fileinput.input(inplace=True)
for line in f:
    line = line.rstrip()
    num = fileinput.lineno()
    print("%-40s # %2i" % (line, num))
f.close()

  注意,只能使用rstrip,不能直接用strip,那样会把左边的缩进也给去掉了。

请在终端环境下,使用python fileinput_test.py fileinput_test.py的方式执行程序,结果如下:

import fileinput                         #  1
                                         #  2
f = fileinput.input(inplace=True)        #  3
for line in f:                           #  4
    line = line.rstrip()                 #  5
    num = fileinput.lineno()             #  6
    print("%-40s # %2i" % (line, num))   #  7
f.close()                                #  8

要小心使用inplace参数,它会修改文件。应该在不适用inplace设置的情况下仔细测试自己的程序(这样只会打印出结果),在确保程序工作正常后再修改文件。  

  

 

  

 

  

  

  

python内置模块(4)的更多相关文章

  1. Python学习笔记【第八篇】:Python内置模块

    什么时模块 Python中的模块其实就是XXX.py 文件 模块分类 Python内置模块(标准库) 自定义模块 第三方模块 使用方法 import 模块名 form 模块名 import 方法名 说 ...

  2. Python内置模块与标准库

    Python内置模块就是标准库(模块)吗?或者说Python的自带string模块是内置模块吗? 答案是:string不是内置模块,它是标准库.也就是说Python内置模块和标准库并不是同一种东西. ...

  3. python内置模块[re]

    python内置模块[re] re模块: python的re模块(Regular Expression正则表达式)提供各种正则表达式的匹配操作,在文本解析.复杂字符串分析和信息提取时是一个非常有用的工 ...

  4. python内置模块[sys,os,os.path,stat]

    python内置模块[sys,os,os.path,stat] 内置模块是python自带功能,在使用内置模块时,需要遵循 先导入在 使用 一.sys 对象 描述 sys.argv 命令行参数获取,返 ...

  5. Python内置模块和第三方模块

    1.Python内置模块和第三方模块 内置模块: Python中,安装好了Python后,本身就带有的库,就叫做Python的内置的库. 内置模块,也被称为Python的标准库. Python 2.x ...

  6. python内置模块collections介绍

    目录 python内置模块collections介绍 1.namedtuple 2.deque 3.defaultdict 4.OrderedDict 5.ChainMap 6.Counter 7.小 ...

  7. python内置模块介绍(一)

     本文主要介绍模块列表如下: os sys re time datetime random shutil subprocess os模块 os.getcwd()                    ...

  8. python内置模块(time模块)

    常用的python内置模块 一.time模块 在python的三种时间表现形式: 1.时间戳,给电脑看的. - 自1970-01-01 00:00:00到当前时间,按秒计算,计算了多少秒. impor ...

  9. python 内置模块续(二)

    目录 python 内置模块补充 1.hashlib模块 简易使用: 高级使用: 进阶使用: 加盐处理: 校验文件一致性 2.logging日志模块 日志等级 常用处理 "四大天王" ...

随机推荐

  1. 使用Git Bash for Windows

    本篇体验Git Bash在Windows操作系统上的用法. 什么是Bash? 是一个Shell环境,Bourne Again Shell的缩写. 安装git for windows → http:// ...

  2. Scrum介绍

    Scrum介绍 摘要 如今,项目管理的步伐越来越快.项目管理需要更灵活.更积极地,向应客户的需求.使用敏捷项目管理方法,项目经理可以在不影响价值.质量和商业规则的前提下实现所有目标,Scrum是一种迭 ...

  3. Java 字符的验证

    package net.hlj.common.util; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @ ...

  4. ios app 提交评审注意事项

    在网络上看到的一个文档是这样写,原文的出处无法确认了 基本要点 ·         不能导致手机故障(比如崩溃或屏幕问题) ·         长时间/过度使用之后反应仍然很快 ·         应 ...

  5. ubuntu11.10搭建eclipse C++开发环境[zhuan]

    1.最重要的东西,C++必要工具,安装的是GCC工具链,Make等一系列开发工具: sudo apt-get install build-essential 2. 安装Eclipse sudo apt ...

  6. python排序算法的实现-冒泡

    1.算法描述: (1)共循环 n-1 次 (2)每次循环中,如果 前面的数大于后面的数,就交换 (3)设置一个标签,如果上次没有交换,就说明这个是已经好了的. 2.代码 #!/usr/bin/pyth ...

  7. LCLFramework架构必须要知道的知识

    技术实现: 代码是否面向对象,要看你的继承怎么用 用抽象来展现层次感 用接口来制定操作的统一性 依赖住入(Inversion of Control) Unit of Work Repository D ...

  8. iOS杂谈-图片拉伸的实现

    如上图是一个按钮的背景图,在Android上,很多图片资源都是类似这样子的,但是由于按钮的高度及宽度与图片的世纪尺寸不同,所以需要采用9patch来实现拉伸处理, 可参考:http://www.cnb ...

  9. vs多项目模板及add-in开发

    本文分2部分 第一为自定义多项目模板 第二为vs add-in开发 效果图 1.自定义模板 2. 工具菜单 3.窗口 4.工程 5.文件 ... 一. 多项目模板 单项目模板做起来很简单 选中一个项目 ...

  10. C# 读取JSON

    引用 Newtonsoft.Json.dll //C# 读取JSON Newtonsoft.Json.Linq.JObject jsonStr = Newtonsoft.Json.Linq.JObje ...