zoukankan      html  css  js  c++  java
  • day22 configparser模块 subprocsee模块 表格

    22天   configparser supcress 表格

     configparser模块

    用于解析配置文件的模块

    配置文件的定义:
    于编写保存某个软件或是某个系统的 一系列参数的文件

    设置 参数
    如果直接写死在程序中,使用者在需要修改参数时 就不得不直接修改源代码

    这是非常不合理的,所以我们通常还会把这些需要变化的参数 放到配置文件

    import configparser
    # 创建 解析对象
    a=configparser.ConfigParser()
    # 读取指定的配置文件
    # read(文件路径,编码)
    a.read('atm.cfg',encoding='utf-8')

    # 获取一个配置项
    # get(分区名称,选项名称) 返回的是字符串
    # getint getfloat getboolean
    count=int(a.get('atm','err_count'))
    print(int(count))

    # configparser模块对配置文件的格式要求
    # section 分区
    # option  选项
    # 所有的value的值不需要用双引号
    # 默认为字符串
    # 不能出现重复的分区
    # 相同分区下不能有重复的option
    # 每一个选项都必须包含在section下

     

    import configparser
    # 常用方法
    a=configparser.ConfigParser()
    a.read('atm.cfg',encoding= 'utf-8')
    # 获取所有分区名称
    print(a.sections())
    # 某个分区下所有option选项名字
    print(a.options('atm'))
    # 封装类型装换的方法
    count=a.getint('atm','err_count')
    print(count)
    # count=a.getfloat('atm','err_count')
    # print(count)
    # count=a.getboolean('atm','err_count')
    # print(count)



    # 不常用方法

    a=configparser.ConfigParser()

    a.read('atm.cfg',encoding= 'utf-8')

    print(a.sections())

    print(a.options('atm'))
    # # 写入
    # with open('atm.cfg','wt',encoding= 'utf-8')as f:
    #     a.write(f)
    '''
    # 设置某个选项的值 如果option已经存在则覆盖
    a.set('atm','test','1471')

    # 错误
    # print(a.set('atm1','test','1471'))
    # 添加一个新分区
    a.add_section('atm1')
    '''

    # 写入
    # with open('atm.cfg','wt',encoding= 'utf-8')as f:
    #     a.write(f)
    print(list(a.keys()))
    print(list(a.values())[2].name)
    # dir可以查看某个对象的所有属性
    print(dir(list(a.values())[2]))
    # 判断是否存在某个标题
    print(a.has_section('section1'))

    # 判断标题section1下是否有选项
    print(a.has_option('section1','user'))

    res=a.has_section('sections')
    if res :
        if a.has_option('section1','user'):
            print(a.get('section1','user'))

    subprocess   子进程


    '''
    subprocess
    子进程

    进程
    指的是一个正在运行中的程序

    子进程指的是由另个一进程开启的进程
    a在运行过程中 开启了b  b就是a的子进程

    为什么要开启子进程
    当一个程序在运行过程中有一个任务,
    自己做不了或是不想做
    就可以开启另一个进程来帮助其完成任务

    可以理解为用于执行系统指令的模块
    '''
    import subprocess
     执行系统指令
    # import os
    # dir 列出所有进程
    # os.system('dir')


    # 打开进程
    # shell 命令解释器
    # stdout  标准输出
    # 指定输出管道
    stdout=subprocess.PIPE

    a=subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE)

    # 从管道中读取出执行结果
    #[1] 输出
    a1=subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE )
    print(a1.stdout.read().decode('GBK'))

    # [2] 输入
    a1=subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE,stdin= subprocess.PIPE)

    # PIPE 大写的单词为常量
    # PIPE = -1
    # STDOUT = -2
    # DEVNULL = -3
    # [3] 错误输出
    print(a1.stderr .read().decode('utf-8'))
    # tasklist | findstr python
    #  先执行tasklist 把结果交给 findstr 来处理

    p1的输出传给p2,
    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 )


    print(p2.stdout.read())
    print(p2.stderr.read())
    # 参数1 指令
    # p = subprocess.Popen("你的指令或是某个exe",shell=True,stderr=,stdin=,stdout=)

    # 2 是否是一个指令
    # 3 错误输出管道
    # 4 输入管道
    # 5 输出管道
    # # 取出管道中的数据
    # p.stderr.read()
    # p.stdout.read()
    # p.stdin.write(p.stdout.read())#
    #  将输入写入管道 交给对方进程

    表格处理

    xlrd 模块是用于读取表格数据的
    xlrd 是一个第三方的需要自己安装 pip install xlrd

    import xlrd

    workbook = xlrd.open_workbook("机密数据.xlsx")
    #查看所有工作表的名称
    print(workbook.sheet_names())

    # 获取某个工作表
    # sheet = workbook.sheet_by_index(1)
    sheet = workbook.sheet_by_name("Sheet1")
    # print(sheet.name)
    # # 获取某一行
    row = sheet.row(2)
    # print(row)
    # # 获取单元格
    cell = row[4]
    # print(cell.ctype) # 单元格的数据类型
    # print(cell.value)#  单元格的数据
    # # 转换日期类型
    print(str(xlrd.xldate_as_datetime(cell.value,0)))
    # 获取表格的列数
    # print(sheet.ncols)
    # # 获取表格的行数
    # print(sheet.nrows)
    # 获取第一行的单元格个数
    # print(sheet.row_len(1))

    # 某个单元格数据
    print(sheet.cell(0,0))

    # 将数据读取出来变成python的数据类型 [{},{},{}]

    # 案例 将边个数据提取为python数据类型
    # 最后的列表
    li = []
    # 先拿出所有的列名称
    keys = sheet.row_values(1)
    for i in range(2,sheet.nrows):
        print(i)
        # row = sheet.row(i)
        # 直接取出所有值
        row = sheet.row_values(i)
        # 创建一个空字典
        dic = {}
        for k in keys:
            # 每次拿出一个key 与一个value 一一对应
            dic[k] = row[keys.index(k)]
            if k == "生日":
                # 如果是生日字段 需要转换时间类型
                dic[k] = str(xlrd.xldate_as_datetime(row[keys.index(k)],0))
        li.append(dic)
    print(li)

     

     

     

     

    生成表格

    import xlwt
    """
    xlwt 是第三方的用于生成一个Exel表格

    """
    # 创建一个工作薄
    wb = xlwt.Workbook()
    # 创建一个工作表
    sheet = wb.add_sheet("特工信息") # type:xlwt.Worksheet

    # 字体对象
    font = xlwt.Font()
    font.bold = True

    # style样式对象
    style = xlwt.XFStyle()
    style.font = font # 将字体设置到样式中

    # 写入数据
    # sheet.write(0,0,"这是标题")
    # 写入 并合并单元格
    sheet.write_merge(0,0,0,4,"这是标题",style)

    # 将工作薄写入到文件
    wb.save("abc.xls")

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    【例题 6-12 UVA
    【例题 6-11 UVA-297】Quadtrees
    【例题 6-10 UVA
    SpringMVC表单验证器
    Spring MVC常用注解
    什么是Spring Boot?
    什么是Kotlin?Java的替代语言?
    阿里Druid连接池的坑。。
    常见的3种Class级别的错误
    阿里巴巴,排行前10的开源项目
  • 原文地址:https://www.cnblogs.com/komorebi/p/10865509.html
Copyright © 2011-2022 走看看