zoukankan      html  css  js  c++  java
  • Python Day5

    一、本节内容 

    模块

    模块,用一砣代码实现了某个功能的代码集合。 

    类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合。而对于一个复杂的功能来,可能需要多个函数才能完成(函数又可以在不同的.py文件中),n个 .py 文件组成的代码集合就称为模块。

    如:os 是系统相关的模块;file是文件操作相关的模块

    模块分为三种:

    • 自定义模块
    • 内置标准模块(又称标准库)
    • 开源模块

    time & datetime模块

     1 import time
     2 print(time.time())
     3 print(time.altzone)                              # 返回与utc时间的时间差,以秒计算
     4 print(time.asctime())
     5 print(time.localtime())                          # 返回本地时间 的struct time对象格式
     6 print(time.gmtime(time.time()))                  # 返回utc时间的struc时间对象格式
     7 
     8 
     9 # 字符串转换为时间戳
    10 print(time.strptime("2017-04-20","%Y-%m-%d"))  # 将字符串转换为struct time对象格式
    11 print(time.mktime(time.strptime("2017-04-20","%Y-%m-%d")))  # 将struct time对象格式转换为时间戳
    12 
    13 
    14 # 将时间戳转换为字符串
    15 print(time.localtime(time.time()))                                      # 将时间戳转换为struct time对象格式
    16 print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())))   # 将struct time对象格式转换为字符串
    17 
    18 
    19 import datetime
    20 
    21 print(datetime.datetime.now())
    22 print(datetime.datetime.fromtimestamp(time.time()))
    23 print(datetime.datetime.now() + datetime.timedelta(3))     # 加3天
    24 print(datetime.datetime.now() - datetime.timedelta(3))     # 减3天
    25 print(datetime.datetime.now() + datetime.timedelta(hours=3))       # 加3小时
    26 print(datetime.datetime.now() + datetime.timedelta(minutes=3))     # 加3分钟
    27 
    28 
    29 print(datetime.datetime.now().replace(hour=3, minute=4))    # 替换时间
    30 print(type(datetime.datetime.now().year))
    31 print(type(datetime.datetime.now()))
    View Code

     

    random模块

     1 #!/usr/bin/env python3
     2 # -*- coding: utf-8 -*-
     3 # Author:Breakering
     4 import random
     5 import string
     6 print(random.random())
     7 print(random.randint(1,5))
     8 print(random.randrange(1,2))                               #不包含右边的,即永远不会输出2
     9 
    10 str_source = string.ascii_letters + string.digits
    11 print(str_source)
    12 print("".join(random.sample(str_source,6)))
    13 
    14 checkcode = ''
    15 for i in range(6):
    16     current = random.randrange(0,6)
    17     if current != i:
    18         temp = chr(random.randint(65,90))
    19     else:
    20         temp = random.randint(0,9)
    21     checkcode += str(temp)
    22 print(checkcode)

    OS模块

     1 #!/usr/bin/env python3
     2 # -*- coding: utf-8 -*-
     3 # Author:Breakering
     4 import os,sys
     5 base_path = os.path.dirname(os.path.abspath(__file__))
     6 sys.path.append(base_path)
     7 print(os.getcwd())                                 #获取当前工作目录,即当前python脚本工作的目录路径
     8 print(os.chdir("ATM"))                             #改变当前脚本工作目录;相当于shell下cd
     9 print(os.getcwd())                                 #获取当前工作目录,即当前python脚本工作的目录路径
    10 print(os.curdir)
    11 print(os.getcwd())
    12 print(os.pardir)
    13 # os.makedirs('dirname1/dirname2')                 #可生成多层递归目录
    14 # os.removedirs('dirname1')                        #若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
    15 # os.mkdir('dirname')                              #生成单级目录;相当于shell中mkdir dirname
    16 # os.rmdir('dirname')                              #删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
    17 print(os.listdir('ATM'))                           #列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
    18 #os.remove("文件名")                               #删除一个文件
    19 #os.rename("oldname","newname")                    #重命名文件/目录
    20 print(os.stat('ATM'))                              #获取文件/目录信息
    21 print(os.sep)                                      #输出操作系统特定的路径分隔符,win下为"\",Linux下为"/"
    22 print(os.linesep)                                  #输出当前平台使用的行终止符,win下为"	
    ",Linux下为"
    "
    23 print(os.pathsep)                                  #输出用于分割文件路径的字符串
    24 print(os.name)                                     #输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
    25 os.system("dir")                                   #运行shell命令,直接显示
    26 print(os.environ)                                  #获取系统环境变量
    27 print(os.path.abspath(__file__))                   #返回path规范化的绝对路径
    28 print(os.path.split(__file__))                     #将path分割成目录和文件名二元组返回
    29 print(os.path.dirname(os.path.abspath(__file__)))  #返回path的目录。其实就是os.path.split(path)的第一个元素
    30 print(os.path.basename(os.path.abspath(__file__))) #返回path最后的文件名。如何path以/或结尾,那么就会返回空值。即os.path.split(path)的第二个元素
    31 print(os.path.exists(os.path.abspath(__file__)))   #如果path存在,返回True;如果path不存在,返回False
    32 print(os.path.isabs(os.path.abspath(__file__)))    #如果path是绝对路径,返回True
    33 print(os.path.isfile(os.path.abspath(__file__)))   # 如果path是一个存在的文件,返回True。否则返回False
    34 print(os.path.isdir(os.path.abspath(__file__)))    #如果path是一个存在的目录,则返回True。否则返回False
    35 print(os.path.join(base_path,"ATM"))               #将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
    36 print(os.path.getatime(os.path.abspath(__file__))) #返回path所指向的文件或者目录的最后存取时间
    37 print(os.path.getmtime(os.path.abspath(__file__))) #返回path所指向的文件或者目录的最后修改时间

    sys模块

     1 #!/usr/bin/env python3
     2 # -*- coding: utf-8 -*-
     3 # Author:Breakering
     4 import sys
     5 print(sys.argv)           #命令行参数List,第一个元素是程序本身路径
     6 print(sys.version)        #获取Python解释程序的版本信息
     7 print(sys.path)           #返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
     8 print(sys.platform)       #返回操作系统平台名称
     9 sys.stdout.write('please:')
    10 sys.stdout.flush()
    11 val = sys.stdin.readline()[:-1]
    12 print(val)
    13 sys.exit()        #退出程序,正常退出时exit(0)

    shutil 模块

     1 #!/usr/bin/env python3
     2 # -*- coding: utf-8 -*-
     3 # Author:Breakering
     4 import shutil
     5 import os
     6 
     7 #将文件内容拷贝到另一个文件中,可以部分内容
     8 # shutil.copyfileobj(open("test1","r",encoding="utf-8"),open("test2","w",encoding="utf-8"))
     9 # shutil.copyfileobj(open("1.png","rb"),open("2.png","wb"))
    10 
    11 #拷贝文件,不知咋用
    12 # shutil.copyfile("test1","test2")
    13 
    14 #仅拷贝权限。内容、组、用户均不变
    15 #shutil.copymode(src, dst)
    16 
    17 #拷贝状态的信息,包括:mode bits, atime, mtime, flags
    18 #shutil.copystat(src, dst)
    19 
    20 #拷贝文件和权限
    21 #shutil.copy(src, dst)
    22 
    23 #拷贝文件和状态信息
    24 #shutil.copy2(src, dst)
    25 
    26 #递归的去拷贝文件
    27 #shutil.copytree(src, dst, symlinks=False, ignore=None)
    28 
    29 #递归的去删除文件
    30 #shutil.rmtree(path[, ignore_errors[, onerror]])
    31 
    32 #递归的去移动文件
    33 #shutil.move(src, dst)
    34 
    35 #创建压缩包并返回文件路径,例如:zip、tar
    36 #shutil.make_archive(base_name, format,...)
    37 path = os.path.dirname(os.path.abspath(__file__))
    38 path1 = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    39 #将path目录下的文件打包放在当前程序目录下
    40 shutil.make_archive("shutil_module_test","zip",root_dir=path)
    41 
    42 import zipfile
    43 #压缩
    44 # z = zipfile.ZipFile('my.zip', 'w')
    45 # z.write("test1")
    46 # z.write("test2")
    47 # z.close()
    48 
    49 #解压
    50 # z = zipfile.ZipFile('my.zip', 'r')
    51 # z.extractall()    #可设置解压地址
    52 # z.close()
    53 
    54 
    55 import tarfile
    56 
    57 # 压缩
    58 tar = tarfile.open('my.tar','w')
    59 tar.add(os.path.join(path1,"时间转换关系图.png"), arcname='时间转换关系图.png')
    60 tar.add(os.path.join(path1,"笔记"), arcname='笔记')
    61 tar.close()
    62 
    63 # 解压
    64 tar = tarfile.open('my.tar','r')
    65 tar.extractall()  #可设置解压地址
    66 tar.close()

    json & pickle 模块

    用于序列化的两个模块

    • json,用于字符串 和 python数据类型间进行转换
    • pickle,用于python特有的类型 和 python的数据类型间进行转换

    Json模块提供了四个功能:dumps、dump、loads、load

    pickle模块提供了四个功能:dumps、dump、loads、load

    总结:json和pickle只能序列化一次

    shelve 模块

    存储

     1 #!/usr/bin/env python3
     2 # -*- coding: utf-8 -*-
     3 # Author:Breakering
     4 
     5 import shelve
     6 
     7 d = shelve.open('shelve_test')  # 打开一个文件
     8 
     9 
    10 def f(*args):
    11     print(args)
    12 
    13 
    14 t1 = f
    15 
    16 name = ["alex", "rain", "test"]
    17 d["test"] = name  # 持久化列表
    18 d["t1"] = t1  # 持久化类
    19 
    20 d.close()

    打开

     1 #!/usr/bin/env python3
     2 # -*- coding: utf-8 -*-
     3 # Author:Breakering
     4 
     5 def f(*args):
     6     print(args)
     7 
     8 import shelve
     9 
    10 d = shelve.open('shelve_test')  # 打开一个文件
    11 
    12 name = d["test"]
    13 t1 = d["t1"]
    14 
    15 d.close()
    16 
    17 print(name)
    18 t1(111)

    总结:shelve可以序列化多次,是以key,values的方式来进行序列化

    xml处理模块

     1 #!/usr/bin/env python3
     2 # -*- coding: utf-8 -*-
     3 # Author:Breakering
     4 import xml.etree.ElementTree as ET
     5 
     6 tree = ET.parse("xml_test.xml")
     7 root = tree.getroot()
     8 print(root.tag)
     9 
    10 # 遍历xml文档
    11 for child in root:
    12     print(child.tag, child.attrib)
    13     for i in child:
    14         print(i.tag, i.text)
    15 
    16         # 只遍历year 节点
    17 for node in root.iter('year'):
    18     print(node.tag, node.text)

    XML修改删除

     1 #!/usr/bin/env python3
     2 # -*- coding: utf-8 -*-
     3 # Author:Breakering
     4 import xml.etree.ElementTree as ET
     5 
     6 tree = ET.parse("xml_test.xml")
     7 root = tree.getroot()
     8 
     9 # 修改
    10 for node in root.iter('year'):
    11     new_year = int(node.text) + 1
    12     node.text = str(new_year)
    13     node.set("updated", "yes")
    14 
    15 tree.write("xml_test1.xml")
    16 
    17 # 删除node
    18 for country in root.findall('country'):
    19     rank = int(country.find('rank').text)
    20     if rank > 50:
    21         root.remove(country)
    22 
    23 tree.write('output.xml')

    XML创建

     1 #!/usr/bin/env python3
     2 # -*- coding: utf-8 -*-
     3 # Author:Breakering
     4 
     5 import xml.etree.ElementTree as ET
     6 
     7 new_xml = ET.Element("namelist")
     8 name = ET.SubElement(new_xml, "name", attrib={"enrolled": "yes"})
     9 age = ET.SubElement(name, "age", attrib={"checked": "no"})
    10 sex = ET.SubElement(name, "sex")
    11 sex.text = '33'
    12 name2 = ET.SubElement(new_xml, "name", attrib={"enrolled": "no"})
    13 age2 = ET.SubElement(name2, "age")
    14 age2.text = '19'
    15 
    16 et = ET.ElementTree(new_xml)  # 生成文档对象
    17 et.write("test.xml", encoding="utf-8", xml_declaration=True)
    18 
    19 ET.dump(new_xml)  #打印生成的格式

    ConfigParser模块

    创建

     1 #!/usr/bin/env python3
     2 # -*- coding: utf-8 -*-
     3 # Author:Breakering
     4 import configparser
     5 
     6 config = configparser.ConfigParser()
     7 config["DEFAULT"] = {'ServerAliveInterval': '45',
     8                      'Compression': 'yes',
     9                      'CompressionLevel': '9'}
    10 
    11 config['bitbucket.org'] = {}
    12 config['bitbucket.org']['User'] = 'hg'
    13 config['topsecret.server.com'] = {}
    14 topsecret = config['topsecret.server.com']
    15 topsecret['Host Port'] = '50022'  # mutates the parser
    16 topsecret['ForwardX11'] = 'no'  # same here
    17 config['DEFAULT']['ForwardX11'] = 'yes'
    18 with open('example.ini', 'w') as configfile:
    19     config.write(configfile)

    增删改查

     1 #!/usr/bin/env python3
     2 # -*- coding: utf-8 -*-
     3 # Author:Breakering
     4 import configparser
     5 
     6 config = configparser.ConfigParser()
     7 config.read('example.ini')
     8 
     9 # ########## 读 ##########
    10 # secs = config.sections()
    11 # print(secs)
    12 # options = config.options('bitbucket.org')
    13 # print(options)
    14 #
    15 # item_list = config.items('bitbucket.org')
    16 # print(item_list)
    17 #
    18 # val = config.get('bitbucket.org','forwardx11')
    19 # val1 = config.getint('bitbucket.org','serveraliveinterval')
    20 
    21 # ########## 改写 ##########
    22 sec = config.remove_section('bitbucket.org')
    23 config.write(open('my.cfg', "w"))
    24 
    25 sec1 = config.has_section('breakering')
    26 print(sec1)
    27 sec2 = config.add_section('breakering')            #只有section不存在才能添加
    28 config.write(open('my.cfg', "w"))
    29 
    30 
    31 config.set('breakering','k1',"11111")
    32 config.set('breakering','k2',"11111")
    33 config.write(open('my.cfg', "w"))
    34 
    35 config.remove_option('breakering','k2')
    36 config.write(open('my.cfg', "w"))

     读

     1 #!/usr/bin/env python3
     2 # -*- coding: utf-8 -*-
     3 # Author:Breakering
     4 import configparser
     5 config = configparser.ConfigParser()
     6 print(config.sections())
     7 config.read("example.ini")
     8 print(config.sections())
     9 
    10 print("bitbucket.org" in config.sections())
    11 
    12 for i in config["bitbucket.org"]:print(i)
    13 
    14 print(config["bitbucket.org"]["serveraliveinterval"])
    15 print(config["bitbucket.org"]["forwardx11"])

    hashlib模块

     1 #!/usr/bin/env python3
     2 # -*- coding: utf-8 -*-
     3 # Author:Breakering
     4 import hashlib
     5 hash = hashlib.sha256()
     6 hash.update(b"abc123")
     7 print(hash.hexdigest())
     8 print(hash.digest())
     9 hash.update(b"456")
    10 print(hash.hexdigest())
    11 
    12 hash2 = hashlib.sha256()
    13 hash2.update(b"abc123456")
    14 print(hash2.hexdigest())
    15 
    16 
    17 import hmac
    18 
    19 h = hmac.new(b"12345","you are 250你是".encode(encoding="utf-8"))
    20 print(h.digest())
    21 print(h.hexdigest())

    Subprocess模块

    os.system
    输出命令结果到屏幕,返回命令状态
    os.popen("dir").read()
    会保存命令的执行结果
     
    >py 2.7
    commands
    commands.getstatusoutput("dir")
     
    subprocess
    subprocess.run()
     

    常用subprocess方法示例

    #执行命令,返回命令执行状态 , 0 or 非0
    >>> retcode = subprocess.call(["ls", "-l"])

    #执行命令,如果命令结果为0,就正常返回,否则抛异常
    >>> subprocess.check_call(["ls", "-l"])
    0

    #接收字符串格式命令,返回元组形式,第1个元素是执行状态,第2个是命令结果
    >>> subprocess.getstatusoutput('ls /bin/ls')
    (0, '/bin/ls')

    #接收字符串格式命令,并返回结果
    >>> subprocess.getoutput('ls /bin/ls')
    '/bin/ls'

    #执行命令,并返回结果,注意是返回结果,不是打印,下例结果返回给res
    >>> res=subprocess.check_output(['ls','-l'])
    >>> res
    b'total 0 drwxr-xr-x 12 alex staff 408 Nov 2 11:05 OldBoyCRM '

    #上面那些方法,底层都是封装的subprocess.Popen
    poll()
    Check if child process has terminated. Returns returncode

    wait()
    Wait for child process to terminate. Returns returncode attribute.


    terminate() 杀掉所启动进程
    communicate() 等待任务结束

    stdin 标准输入

    stdout 标准输出

    stderr 标准错误

    pid
    The process ID of the child process.

    #例子

    1 >>> p = subprocess.Popen("df -h|grep disk",stdin=subprocess.PIPE,stdout=subprocess.PIPE,shell=True)
    2 >>> p.stdout.read()
    3 b'/dev/disk1 465Gi 64Gi 400Gi 14% 16901472 104938142 14% /
    '

    logging模块

    初级

     1 #!/usr/bin/env python3
     2 # -*- coding: utf-8 -*-
     3 # Author:Breakering
     4 import logging
     5 
     6 logging.basicConfig(filename='log.log',
     7                     format='%(asctime)s - %(name)s - %(levelname)s - %(module)s - %(lineno)d:  %(message)s',
     8                     datefmt='%Y-%m-%d %H:%M:%S %p',
     9                     level=10)
    10 
    11 logging.debug('debug')
    12 logging.info('info')
    13 logging.warning('warning')
    14 logging.error('error')
    15 logging.critical('critical')
    16 logging.log(10, 'log')
    View Code

    中级

     1 #!/usr/bin/env python3
     2 # -*- coding: utf-8 -*-
     3 # Author:Breakering
     4 import logging
     5 
     6 # create logger
     7 logger = logging.getLogger('TEST-LOG')
     8 logger.setLevel(logging.DEBUG)
     9 
    10 # create console handler and set level to debug
    11 ch = logging.StreamHandler()
    12 ch.setLevel(logging.DEBUG)
    13 
    14 # create file handler and set level to warning
    15 fh = logging.FileHandler(filename="access.log")
    16 fh.setLevel(logging.WARNING)
    17 # create formatter
    18 ch_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    19 fh_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(module)s - %(lineno)d:  %(message)s')
    20 
    21 # add formatter to ch and fh
    22 ch.setFormatter(ch_formatter)
    23 fh.setFormatter(fh_formatter)
    24 
    25 # add ch and fh to logger
    26 logger.addHandler(ch)
    27 logger.addHandler(fh)
    28 
    29 if __name__ == "__main__":
    30     # 'application' code
    31     logger.debug('debug message')
    32     logger.info('info message')
    33     logger.warning('warn message')
    34     logger.error('error message')
    35     logger.critical('critical message')
    View Code

    高级

     1 #!/usr/bin/env python3
     2 # -*- coding: utf-8 -*-
     3 # Author:Breakering
     4 import logging
     5 
     6 from logging import handlers
     7 
     8 logger = logging.getLogger(__name__)
     9 
    10 log_file = "timelog.log"
    11 fh = handlers.RotatingFileHandler(filename=log_file,maxBytes=1073741824,backupCount=3)
    12 #fh = handlers.TimedRotatingFileHandler(filename=log_file,when="S",interval=5,backupCount=3)
    13 
    14 
    15 formatter = logging.Formatter('%(asctime)s %(module)s:%(lineno)d %(message)s')
    16 
    17 fh.setFormatter(formatter)
    18 
    19 logger.addHandler(fh)
    20 
    21 
    22 logger.warning("test1")
    23 logger.warning("test12")
    24 logger.warning("test13")
    25 logger.warning("test14")
    View Code

    日志格式

    %(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

    用户输出的消息

    re模块

    常用正则表达式符号

     1 '.'     默认匹配除
    之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行
     2 '^'     匹配字符开头,若指定flags MULTILINE,这种也可以匹配上(r"^a","
    abc
    eee",flags=re.MULTILINE)
     3 '$'     匹配字符结尾,或e.search("foo$","bfoo
    sdfsf",flags=re.MULTILINE).group()也可以
     4 '*'     匹配*号前的字符0次或多次,re.findall("ab*","cabb3abcbbac")  结果为['abb', 'ab', 'a']
     5 '+'     匹配前一个字符1次或多次,re.findall("ab+","ab+cd+abb+bba") 结果['ab', 'abb']
     6 '?'     匹配前一个字符1次或0次
     7 '{m}'   匹配前一个字符m次
     8 '{n,m}' 匹配前一个字符n到m次,re.findall("ab{1,3}","abb abc abbcbbb") 结果'abb', 'ab', 'abb']
     9 '|'     匹配|左或|右的字符,re.search("abc|ABC","ABCBabcCD").group() 结果'ABC'
    10 '(...)' 分组匹配,re.search("(abc){2}a(123|456)c", "abcabca456c").group() 结果 abcabca456c
    11  
    12  
    13 'A'    只从字符开头匹配,re.search("Aabc","alexabc") 是匹配不到的
    14 ''    匹配字符结尾,同$
    15 'd'    匹配数字0-9
    16 'D'    匹配非数字
    17 'w'    匹配[A-Za-z0-9]
    18 'W'    匹配非[A-Za-z0-9]
    19 's'     匹配空白字符、	、
    、
     , re.search("s+","ab	c1
    3").group() 结果 '	'
    20  
    21 '(?P<name>...)' 分组匹配 re.search("(?P<province>[0-9]{4})(?P<city>[0-9]{2})(?P<birthday>[0-9]{4})","371481199306143242").groupdict("city") 结果{'province': '3714', 'city': '81', 'birthday': '1993'}

    最常用的匹配语法

    1 re.match 从头开始匹配
    2 re.search 匹配包含
    3 re.findall 把所有匹配到的字符放到以列表中的元素返回
    4 re.splitall 以匹配到的字符当做列表分隔符
    5 re.sub      匹配字符并替换 

    反斜杠的困扰
    与大多数编程语言相同,正则表达式里使用""作为转义字符,这就可能造成反斜杠困扰。假如你需要匹配文本中的字符"",那么使用编程语言表示的正则表达式里将需要4个反斜杠"\\":前两个和后两个分别用于在编程语言里转义成反斜杠,转换成两个反斜杠后再在正则表达式里转义成一个反斜杠。Python里的原生字符串很好地解决了这个问题,这个例子中的正则表达式可以使用r"\"表示。同样,匹配一个数字的"\d"可以写成r"d"。有了原生字符串,你再也不用担心是不是漏写了反斜杠,写出来的表达式也更直观。

    了解几个匹配模式

    1 re.I(re.IGNORECASE): 忽略大小写(括号内是完整写法,下同)
    2 M(MULTILINE): 多行模式,改变'^''$'的行为(参见上图)
    3 S(DOTALL): 点任意匹配模式,改变'.'的行为

    二、本节作业    

    开发一个简单的python计算器

    1. 实现加减乘除及拓号优先级解析
    2. 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )等类似公式后,必须自己解析里面的(),+,-,*,/符号和公式(不能调用eval等类似功能偷懒实现),运算后得出结果,结果必须与真实的计算器所得出的结果一致

     

  • 相关阅读:
    如何打开肉鸡的3389端口(xp的)
    XP鲜为人知的实用技巧(一)
    利用Ms08067工具进行溢出攻击
    教你建一个别人看不到打不开的文件夹
    在IE上显示自己的名字
    QQ使用的七大非常规秘籍
    第五篇:Python函数基础篇
    Centos7之Systemd(Service文件)详解
    Linux GCC make文件的写法3
    DSP/BIOS学习笔记——2.SWI
  • 原文地址:https://www.cnblogs.com/breakering/p/6773200.html
Copyright © 2011-2022 走看看