zoukankan      html  css  js  c++  java
  • day21

    今日所学:

             1,configparser 模块

             2,subprocess   模块

             3, 表格处理相应的模块

         一:configparser:用来解析配置文件的模块。对配置文件有格式要求

              只能有:分区    section

            选项    option

              同一个section不能有重复的option

       不能有重复的section  

              不区分数据类型,都是字符串,任何的option都必须在section内包含

             重要的方法:

                            read(文件名,编码)例如  c.read("atm.cfg")     c是

                            get(分区名称,选项名称)返回的是字符串,例如c.get("atm","name")

                    getint 为整形  getfloat 为浮点型  getboolean 为布尔型 

            一般用户来写  程序来读:

                      

                     import configparser

    cc = configparser.ConfigParser()
    cc.add_section("test")
    cc.set("test", "name", "fzy")
    with open('mun.cfg', 'wt', encoding='utf-8') as f:
    cc.write(f)

                            结果为:在mun.cfg文件中

                      [test]
    name = fzy
    语法:
                 import configparser
    #创建解析对象
    c = configparser.ConfigParser()
    c.read("mun.cfg", encoding='utf-8') #读取指定的配置文件
                  #获取一个配置项
    # count = c.get("atm", "e_count")
    # print(count)
    # count = c.getint("atm","e_count")
    # print(count)
    #获取所有分区名称
    print(c.sections()) #结果为['test']
    #某个分区下所有option 名字
    print(c.options("test")) #结果为['name']
    ########
            print(list(c.keys()))          #结果为 ['DEFAULT', 'test']v
            print(list(c.values())) #结果为[<Section: DEFAULT>, <Section: test>]
          # dir 可以查看某个对象所有可用的属性 _ _开头不要管,系统自带
            print(dir(list(c.values())))# 结果很多。。。。。。'append', 'clear', 'copy', 'count', '。。。。
    二:subprocess   模块=================================================================================重点
    子进程 进程;一个正在运行中的程序
    进程的进程,即由另一个进程开启的进程,a在运行过程中开启了b,b就是a的子进程
    为什么要开启子进程:一个程序在运行过程中有一个任务自己做不了,或者不想做,就需要另一个进程来帮助其完成任务
    可以理解为用于执行系统指令的模块
    import subprocess =======================================================================
    内存中每个进程的内存是相互隔开的,不能直接访问,所以需要管道来通讯
    # stdout=subprocess.PIPE 输出管道
    ======================= p = subprocess.Popen("dir", shell=True, stdout=subprocess.PIPE)================是重点
    #print(p.stdout.read()) #从官到里面读数据,这样读出来都是字节
    #从管道中读取出执行结果
    =======================reuslt = (p.stdout.read().decode("GBK"))========================================是重点


    #三个管道 ====== :输出管道:stdout=subprocess.PIPE,输入管道: stdin=subprocess.PIPE,错误管道: stberr=subprocess.PIPE===============================
    ================ #p1 = subprocess.Popen("dir", shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)==============
    ================# p1 = subprocess.Popen("dirs", shell=True, stdout=subprocess.PIPE, stderr=-1)==================================================
    ===== # print(p1.stderr.read().decode('GBK')) #错误管道中的输出信息,'dirs' 不是内部或外部命令,也不是可运行的程序或批处理文件=======================
    ===== # 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)
    print(p2.stdout.read())
    print(p2.stderr.read())
    subprocess 的掌握方法:==============================================================================================
    1,参数即指令 你的指令或者某个exe文件"
    2,是否是一个指令
    3,错误输出管道 stderr=subprocess.PIPE
    4,输入管道 stdin=subprocess.PIPE
    5输出管道 stdout=subprocess.PIPE
    subprocess.Popen("你的指令或者某个exe文件", shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)============================================#取出管道中的数据p.stderr.read()
    p.stdout.read()
    #将输入写入管道,交给对方进程
    p.stdin.write(p.stdout.read())
    #你需要系统执行指令时,就需要它
    三: 表格处理相应的模块
    是python中一个第三方的用于读取excle表格的模块
    xlrd模块
    import xlrd
    读取文件
    work_book = xlrd.open_workbook("/Users/jerry/Desktop/公司机密数据.xlsx")
    选取一个表
    获取所有所有表格名称
    print(work_book.sheet_names())
     选择第2个  索引从0开始
    sheet = work_book.sheet_by_index(1)
    表格名称
    print(sheet.name)
    行数
    print(sheet.nrows)
    列数
    print(sheet.ncols)

    批量读取行数据
     取出第6行的全部内容包含数据类型
    print(sheet.row(6))
    取出第6行的内容包含数据类型 从第3列开始获取
    print(sheet.row_slice(6,start_colx=3))
    取出第6行的内容包含数据类型 从第3列开始获取
    print(sheet.row_slice(6,start_colx=4,end_colx=5))
     获取该行所有数据类型 一数字表示
    print(sheet.row_types(6))
    print(sheet.row_values(6))

    单元格的处理
    print(sheet.cell(0,0).value) # 取值
    print(sheet.cell(0,0).ctype) # 取类型
    print(sheet.cell_value(2,0)) # 直接取值
    print(sheet.row(0)[0]) # 先取行再取单元格
    print(sheet.col(0)) # 第0列所有数据
    print(sheet.col(0)) # 先取列再取单元格
    print(sheet.cell_type(0,0))
    单元格位置转换
    print(xlrd.cellname(2,1))
    print(xlrd.cellnameabs(0,2))
    print(xlrd.colname(5))
    xlwt模块
    import xlwt
    创建工作簿
    work = xlwt.Workbook()
     创建一个表
    sheet = work.add_sheet("员工信息数据")

    创建一个字体对象
    font = xlwt.Font()
    font.name = "Times New Roman" # 字体名称
    font.bold = True # 加粗
    font.italic = True # 斜体
    font.underline = True # 下划线
    #创建一个样式对象
    style = xlwt.XFStyle()
    style.font = font

     写入标题
    for k in keys:
        sheet.write(0,keys.index(k),k,style)
    写入数据
    for i in infos:
        for k in keys:
            sheet.write(1 + infos.index(i),keys.index(k),label = i[k])
    保存至文件
    work.save("test.xls")




  • 相关阅读:
    Singleton(单例模式)的一种实现 -- 基于【惰性】适用于【大对象】的一种生产实践
    001.Getting Started -- 【入门指南】
    SparkStreaming高级算子应用【combineByKey、transform,checkpoint】
    Solr基础理论【相关度计算】
    Solr基础理论【排名检索、查准率、查全率】
    Solr基础理论【倒排索引,模糊查询】
    Impala快速入门
    Redis特点分析及性能优化
    电力系统【第八章:电力系统不对称故障的分析与计算】
    SparkStreaming之checkpoint检查点
  • 原文地址:https://www.cnblogs.com/Fzhiyuan/p/10864225.html
Copyright © 2011-2022 走看看