5.14—022A—周二

今日所学内容

一、configparser  用于解析配置文件 的模块

配置文件的定义

  用于编写保存某个软件或是某个系统的一系列参数的文件

设置参数

为什么需要配置文件:无论是什么样的软件应用程序,在执行的过程中都需要很多的参数,而且有一些参数需要经常修改

  Eg:QQ里面的下载路径; ATM中的错误次数。  如果直接写死在程序中,使用者在需要修改参数时就不得不直接修改代码源这是非常不合理的额,所以我们通常会把这些需要变化的参数放入到配置文件中

打开配置文件来读取参数

  with open("atm.cfg","r") as r

    err_count = int(f.read())

创建 解析对象

  import configparser

  c = configparser.Configparser()

读取指定的配置文件

  c.read("atm.cfg", encoding= "utf-8")

获取一个配置项

  count = int(c.get("atm","err_count"))

configparser是用来配置文件的,对配置文件有格式要求:

  文件格式只能有分区 section 和选项 option

  在一个配置文件中只能有一个section,不能有重复的section

  在一个分区section中只能有一个option,不能有重复的option

  在配置文件中,不区分数据类型,都是字符串

  “#”可以用来注释

  所有的 option都必须包含在 section 中

需要掌握的主要方法:

  read(文件路径,编码)

  get(分区名称,选项名称) 返回的字符串

  getint  getfloat    getboolean

其它方法

  import configparser

  c = configparser.Configparser()

  c.read("atm.cfg", encoding= "utf-8")

获取所有的分区名称

  print(c.section())

获取某个分区下的所有option 名字

  print(c.option(“分区名”))

读写:

  c.set("atm","test","123")  设施某个选项的值,如果option不存在新建,存在则覆盖

  c.add_section("atm")    添加一个新的分区

获取所有的 keys

  print(list(c.keys()))

获取所有的 velues

  print(list(c.values()))

dir 可以查看某个对象所有可用的属性, __开头的不用管,系统自带

  print(dir(list(c.values())[1]))

*判断是否存在某个标题
  print(config.has_section('section1'))

*判断标题section1下是否有user
  print(config.has_option('section1',''))

二、subprocecess 子进程

subprocess 称之为子进程,进程是一个正在运行的程序

为什么要使用子进程,因为之前的os.system()函数无法获取命令的执行结果,另一个问题是当我们启动了某一其他进程时无法与这个子进程进行通讯

当要在python程序中执行系统指令时 就应该使用subprocess 自动化运维经常会使用

 

测试
  res = os.system("python")
  print(res)      res结果为执行状态

  import subprocess

  p = subprocess.Popen("ls",shell=True)

shell=True 告诉系统这是一个系统指令 而不是某个文件名

此时效果与sys.system()没有任何区别,都是将结果输出到控制台

那如何与这个进程交互数据呢,这需要用到三个参数
  1.stdin  表示输入交给子进程的数据
  2.stdout 表示子进程返回的数据
  3.stderr 表示子进程发送的错误信息

这三个参数,的类型都是管道,(管道本质就是一个文件,可以进行读写操作),使用subprocess.PIPE来获取一个管道

总结: subprocess 主要用于执行系统命令,对比sys.system 区别在于可以在进程间交换数据

三、mlrd模块: Excel读

  import xlrd
*读取文件
  work_book = xlrd.open_workbook("机密数据.xlsx")

获取所有所有表格名称
  print(work_book.sheet_names())

选取一个表
  sheet = work_book.sheet_by_index(1)

表格名称
  print(sheet.name)

*行数
  print(sheet.nrows)

*列数
  print(sheet.ncols)

某行全部
  print(sheet.row(6))

某列全部
  print(sheet.col(6))

某行列区间
  print(sheet.row_slice(6, start_colx=0, end_colx=4))

某列行区间
  print(sheet.col_slice(3, start_colx=3, end_colx=6))

某行类型 | 值
  print(sheet.row_types(6), sheet.row_values(6))

单元格
  print(sheet.cell(6,0).value)   # 取值
  print(sheet.cell(6,0).ctype)   # 取类型
  print(sheet.cell_value(6,0))   # 直接取值
  print(sheet.row(6)[0])

时间格式转换
  print(xlrd.xldate_as_datetime(sheet.cell(6, 0).value, 0))

四、xlwt模块:excel写

  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
  keys = ['Owen', 'Zero', 'Egon', 'Liuxx', 'Yhh']

写入标题
  for k in keys:

    sheet.write(0, keys.index(k), k, style)

写入数据
  sheet.write(1, 0, 'cool', style)

保存至文件
  work.save("test.xls")

猜你喜欢

转载自www.cnblogs.com/Chinesehan/p/10865130.html
022