Python脱产8期 2019/5/14

一 confiparser模块

1.定义:用于解析配置文件的模块

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

例如: qq里面的下载路径 ATM中的错误次数

3.语法:

  1.

import configparser
# 创建 解析对象
c = configparser.ConfigParser()
c.read("atm.cfg",encoding="utf-8") # 读取指定的配置文件
# 获取一个配置项
count = int(c.get("atm","err_count"))
print(int(count))

2.封装了类型转换的方法
# count = c.getint("atm","err_count")
# c.getfloat()
# c.getboolean()
3.常用功能语法

 1..#查看所有的分区
 res=config.sections() #['section1', 'section2']
 print(res)

 2.#查看标题section1下所有key=value的key
  options=config.options('section1')
  print(options) #['k1', 'k2', 'user', 'age', 'is_admin', 'salary']

 3.#查看标题section1下所有key=value的(key,value)格式
  item_list=config.items('section1')
  print(item_list) #[('k1', 'v1'), ('k2', 'v2'), ('user', 'egon'), ('age', '18'), ('is_admin', 'true'), ('salary', '31')]

 4.#是否存在某选项
print(cfg.has_option("mysql","name"))
 5.#是否存在某分区
 print(cfg.has_section("db"))

4.添加,删除,修改语法

#删除整个标题section2
config.remove_section('section2')

#删除标题section1下的某个k1和k2
config.remove_option('section1','k1')
config.remove_option('section1','k2')

#添加一个标题
config.add_section('jack')

#在标题egon下添加name=egon,age=18的配置
config.set('jack','name','egon') # 如果已存则覆盖原来的值
#config.set('jack','age',18) #报错,必须是字符串

#最后将修改的内容写入文件,完成最终的修改
config.write(open('a.cfg','w'))

#添加一个标题
config.add_section('jack')

#在标题egon下添加name=egon,age=18的配置
config.set('jack','name','egon') # 如果已存则覆盖原来的值
#config.set('jack','age',18) #报错,必须是字符串

#最后将修改的内容写入文件,完成最终的修改
config.write(open('a.cfg','w'))

代码创建生成文件

import configparser
c = configparser.ConfigParser()

c.add_section("test")
c.set("test","name","jack")

with open("test.cfg","wt",encoding="utf-8") as f:
c.write(f)
总结:
configparser 用来解析配置文件的 
对配置文件有各式要求
只能由分区和选项
section 和 option
同一个section' 不能有重复的option
不能有重复的section
不区分数据类型 都是字符串
# 可以用来注释
任何option都必须包含在section

二 subprocess的使用

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

2.为什么要开启子进程:当一个程序在运行过程中有一个任务,自己做不了或是不想做 就可以开启另一个进程来帮助其完成任务例如 qq中收到一个链接 点击链接 就开启了; 浏览器 浏览器就是qq的子进程

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

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

4.语法:

import subprocess

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

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

5.案例:

一个子进程执行tasklist命令获取所有的任务信息,然后将结果交给另一个进程进行查找

另一个子进程执行findstr 查找某个任务信息

p1 = subprocess.Popen("tasklist",shell=True,stdout=subprocess.PIPE)
p2 = subprocess.Popen("findstr smss",shell=True,stdin=p1.stdout,stdout=subprocess.PIPE)
print(p2.stdout.read())

三 表格处理

1.什么是xlrd(读取数据):

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

2.语法

打开文件

wb = xlrd.open_workbook("路径")
# 获取某个表格
sheet = wb.sheet_by_name()
sheet = wb.sheet_by_index()

# 获取行数
sheet.nrows()
# 获取列数
sheet.ncols()

# 取某行数据
sheet.row_values(行索引)

# 获取某单元格的数据
sheet.cell(行,列).value

四 表格处理(生成)

1.什么是: xlwt 是第三方的用于生成一个Exel表格

2.语法

import xlwt
# 创建一个工作薄
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")



 


 
 

 

猜你喜欢

转载自www.cnblogs.com/tfzz/p/10864160.html
今日推荐