设为首页 加入收藏

TOP

NO.5:自学python之路------标准库,正则表达式(二)
2018-10-19 16:52:20 】 浏览:116
Tags:NO.5 自学 python 之路 ------ 标准 正则 表达式
il.copy2('test1','test2')#复制文件和状态 shutil.copytree('test1','test2')#递归复制,复制目录下所有文件 shutil.rmtree('test2')#递归删除,删除目录下所有文件 shutil.move('test1','test2')#递归的移动文件 shutil.make_archive('压缩包文件名或路径','压缩包种类zip,tar') View Code

 

zipfile/tarfile

 

import zipfile
z = zipfile.ZipFile('test.zip','w')
z.write('test1.py')
z.close()
z = zipfile.ZipFile('test.zip','r')
z.extractall()
z.close()
View Code

shelve

import shelve#内存数据持久化模块
d = shelve.open('test')
class test(object):
    def __init__(self,n):
        self.n = n
t = test(123)
t2 = test(1233)
name = ['aa','bb','cc']
d['test'] = name #持久化列表
d['t1'] = t #持久化类
d['t2'] = t2
d.close()
d = shelve.open('test')#打开shelve
d.get('t1')#读出t1
View Code

configparser

 

import configparser#配置文档
#生成
config = configparser.ConfigParser()
config['DEFAULT'] = {'LALAL':'23','AAFF':'22'
}
config['NEW'] = {}
with open('test.ini','w') as configfile:
   config.write(configfile)
#读取
config = configparser.ConfigParser()
config.read('test.ini')
config.sections()#打印节点
config['new']['lala']
#删除
sec = conf.remove_section('NEW')
conf.write(open('test.ini','w'))
View Code

 

hashlib

 

import hashlib#hashlib用于加密,md5
m = hashlib.md5()
m.update(b'hello')
m.update(b'new')
print(m.hexdigest())#sha1\sha512\sha256....
View Code

 

hmac

 

import hmac
h = hmac.new(b'key','信息'.encode(encoding=utf-8))
print(h.digest())
print(h.hexdigest())
View Code

 

正则表达式re

 

import re
#匹配规则:
#'.'匹配除'\n'以外的任意字符,指定flag DOTALL可匹配任意字符;
#'^'匹配字符开头,指定flag MULTLTNE,可以匹配('a','\nabc\neee',flag=re.MULTILINE)
#'$'匹配结尾
#'*'匹配*前的字符0次或多次,re.findall('ab*','cabb3abcbaac')
#'+'匹配前一个字符一次或多次
#'?'匹配前一个字符1次或0次
#'{m}'匹配前一个字符m次
#'{n,m}'匹配前一个字符n到m次
#'|'匹配'|'左或右的字符
#'(...)'分组匹配
#'\A'只从字符开头匹配
#'\Z'匹配字符结尾
#'\d'匹配数字0~9
#'\D'匹配非数字
#'\w'匹配[A-Za-z0-9]
#'\w'匹配非[A-Za-z0-9]
#'\s'匹配空白字符、\t、\n、\r
re.search()#全局搜索,取到一个后返回
re.match()#从头搜索,取到一个后返回
re.findall()#取出全部后返回
re.split()#分割
re.sub()#替换
View Code

 

作业

  Python字符串计算器

  功能:

  1.输入字符串,计算

  2.括号,乘除,优先级

#-*- coding:utf-8 -*-
#author: Kai Zhang
#string calculation

import re

def mul(factor_1,factor_2):
    '''乘法'''
    value = float(factor_1) * float(factor_2)
    return str(value)

def div(factor_1,factor_2):
    '''除法'''
    value = float(factor_1) / float(factor_2)
    return str(value)

def add(factor_1,factor_2):
    '''加法'''
    value = float(factor_1) + float(factor_2)
    return str(value)

def sub(factor_1,factor_2):
    '''减法'''
    value = float(factor_1) - float(factor_2)
    return str(value)

def muldiv(formula):
    '''计算所有乘除'''
    formula = formula.replace('/*','/')
    formula = formula.replace('*/','/')
    muldiv_flag = True
    while muldiv_flag:
        inside = re.search('\d+\.?\d*[\*\/]-?\d+\.?\d*',formula)
        if inside:
            inside = inside.group()
            number = re.findall('\d+\.?\d*',inside)
            if '-' in inside:
                number[1] = '-' + number[1]
            if '*' in inside:
                value = mul(number[0],number[1])
            else:
                value = div(number[0],number[1])
            formula = formula.replace(inside,value)
        else:
            muldiv_flag = False
    return formula

def addsub(formula):
    '''计算所有加减'''
    formula = formula.replace('--','+')
    formula = formula.replace('-+','-')
    formula = formula.replace('+-','-')
    formula = formula.replace('++','+
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇django 分页出现 UnorderedObject.. 下一篇Python3正则表达式(2)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目