zoukankan      html  css  js  c++  java
  • Python Day22:Configparse、Subprocess、Xlrd、Xlwt模块

    ## configparser模块
    
    ~~~python
    用于解析配置文件的模块:
        应用程序/软件,在执行过程中,都需要很多参数,(如qq的下载路径/记住密码功能),很多都会需要修改,不能写死在程序中,所以我们会把这些需要变化的参数放到配置文件放到配置文件中.configparser模块就是来方便解析这些文件的.
    不用configparser模块来解析也可以读取配置文件:
        例:with open("targetfile","r",encoding="utf-8")as fr:
                count=f.read()
                ``````(将文件转换成目标类型格式)``````
    上方法比较繁琐```
    configparser:
        import configparser#导入模块
        #创建解析对象
        c=configparser.ConfigParser()
        #对象读取配置文件---->(将内容读取到内存?)
        c.read("targetfile",encoding="utf-8")
        #获取配置文件的具体配置项section->option
        count=c.get("section","option")
        print(count)print(type(count)
    #注意事项:
           配置文件格式要求:
            必须有section |option,且option必须包含在section中
              不能有重复section,同一个section不能有重复option
            不区分数据类型,都是字符串不需要额外加""
            可以用#来注释
     #常用方法: 
     #获取所有分区名称:c.sections()
     #获取某个分区所有option:c.options("section")
     #封装类型转换方式:getint() |getfloat() |getboolean()
     #添加一个新分区:c.add_section("section")
     #添加覆盖分区内某个选项:c.set("section","option","值")
    添加覆盖等写入操作,需要配合with open 来写入,否则无法写入
        with open("test.cfg","wt",encoding="utf-8") as f:
            c.write(f)#c.write("文件对象,并非路径!")
                      #read("文件名文件路径")
      #configparser模块,读写配置文件时,都是读写内存信息,然后保存在内存中,写的时候,必须在配合withopen来打开操作系统的文件.
    ~~~
    
    ## subprocess模块
    
    ```python
    子进程:由另一个进程开启的进程.a在运行过程中开启了b,不就是a的子进程.
    为什么开启子进程,一个程序在运行过程中,有任务自己做不了或不想做,就可以开启另一个进程来帮助完成任务
    #在内存中每个进程的内存区域都是互相隔离的,不能直接访问,所以需要管道来通讯
    #python中有三个管道:stdout,stderr,stdin
    #stdout=subprocess.PIPE就是指定了一个输出管道
    #p=subprocess.Popen("dir",shell=True,stdout=sbuprocess.PIPE)
    #从管道中读取执行结果:result=p.stdout.read().decode("GBK")
    p1=subprocess.Popen("dirs",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    print(p1.stdout.read())
    print(p1.stderr.read().decode("GBK"))
    案例:
        tasklist |findstr python#先执行tasklist,把结果交给    findstr来处理.
       p1=subprocess.Popen("tasklist",shell=True,stdout=subprocess.PIPE)
    p2 = subprocess.Popen("findstr QQ",shell=True,stdin=p1.stdout,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    #Popen方法,用来打开指令,进入子进程
    print(p2.stdout.read())
    print(p2.stderr.read())
    
    p = subprocess.Popen("你的指令或是某个exe",shell=True,stderr=,stdin=,stdout=)
    # 将输入写入管道 交给对方进程
    # p.stdin.write(p.stdout.read())
    当需要执行系统指令时 需要想起它!
    ```
    
    ## xlrd模块
    
    ```python
    #读取Excel表格信息的模块.
    #创建读取表格对象
    workbook=xlrd.open_workbook("表格数据.xls"文件路径)
    #获取表格文件的某个工作表sheet
    sheet=workbook.sheet_by_name("工作表名")
    sheet = wb.sheet_by_index("索引值")
    #表下面的方法:
        n=sheet.nrows#获取表的行数
        n=sheet.ncols#获取表的列数
        rown=sheet.row(n)#获取某一行的所有数据
        values=sheet.row_values(n)#获取某一行的说有数据的值,返回一个列表
        sheet.row_len(n)#获取某一行的单元格个数
        sheet.cell("行数","列数")#获取具体单元格数据
        cell.value#单元格的值
       获取的单元格数据如下: 
      [text:'姓名', text:'年龄', text:'性别', text:'籍贯'] 
        
        
        
    ```
    
    ## xlwt模块
    
    ```python
    用于生成一个表格,并对表格进行数据写入(很少用到,了解)
    #创建表格对象
    we=xlwt.Workbook()
    #创建工作表对象
    sheet=we.add_sheet("输入内容")
    # 字体对象
    font = xlwt.Font()
    font.bold = True
    # style样式对象
    style = xlwt.XFStyle()
    style.font = font # 将字体设置到样式中
    
    
    #写入数据:
    sheet.write(行数,列数 ,"输入内容")
    #写入并合并单元格
    sheet.write_merge(起始行,结束行,起始列,结束列,"输入内容",style)
    #将工作薄写入到文件
    we.save("abc.xls")
    ```
  • 相关阅读:
    Android 目前最稳定和高效的UI适配方案
    很值得收藏的安卓开源控件库
    django-初始配置(纯手写)
    面向对象 继承
    wsgiref手写一个web服务端
    socket手写一个简单的web服务端
    vue指令
    vue入门
    python中and,or
    面向对象三大特性-继承
  • 原文地址:https://www.cnblogs.com/huhongpeng/p/10922790.html
Copyright © 2011-2022 走看看