day22

configparser:用于解析配置

#配置文件:用于编写保存某个软件或是某个系统的一系列参数的文件 配置文件的后缀一般是.cfg
#配置文件作用:无论是什么样的软件应用程序 在执行的过程中 都需要很多的参数 而一些参数经常会需要修改

import configparser
sfg = configparser.ConfigParser() #配置文件对象

#配置文件写法:section 分区, option 选项,配置文件中存放的数据全是字符串类型
#不能有重复的section 同一个section不能有重复的option,可以用'#'来注释,option必须包含在section里
[secion]
option = 'xxx'
#常用函数
cfg.read('文件路径','设置编码格式')
cfg.wirte('文件对象')
cfg.sctions('获取所有分区')
cfg.options('获取某个分区下的所有选项')
cfg.get('获取某个分区下的某个选项')
cfg.getint('获取某个分区下使用时需要转换成int的数据')
cfg.getboolean('获取某个分区下使用时需要转换成bool的数据')
cfg.getfloat('获取某个分区下使用时需要转换成float的数据')
cfg.has_option('是否存在某个选项')
cfg.has_section('是否存在某个分区')
cfg.remove_section('删除一个分区')
cfg.remove_option('删除一个选项')
cfg.add_section('添加一个标题')
cfg.set('分区','选项','值')
#configparser 用于解析配置文件,虽然可以修改和,创建,配置文件,但是并不常用,解析才是其核心功能!

xml

subprocess:子进程,用于执行系统指令的模块

#进程:一个正在运行中的程序
#子进程:由一个进程开启的进程
#当一个程序在运行过程中有一个任务,自己做不了或是不想做,就可以开启另一个进行来帮助其完成任务
import subprocess
subprocess.Popen('ls',shell = Ture)
#shell=True 告诉系统这是一个系统指令 而不是某个文件名
#此时效果与sys.system()没有任何区别,都是将结果输出到控制台
#stdin 表示输入交给子进程的数据
#stdout 表示子进程返回的数据
#stderr 表示子进程发送的错误信息
#这三个参数的类型都是管道,(管道本质就是一个文件,可以进行读写操作),使用subprocess.PIPE来获取一个管道
#因为内存中的分区的,每个进程相互隔离,所以我们需要用到管道来保证各个进程之间的数据交互
p1 = subprocess.Popen("tasklist",shell=True,stdout=subprocess.PIPE) #PIPEs是一个常量,值为-1
p2 = subprocess.Popen("findstr smss",shell=True,stdin=p1.stdout,stdout=subprocess.PIPE)
print(p2.stdout.read())

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

xlrd:用于读取excle表格的数据

xlwt:用于写入excle数据到表格

#xlrd和xlwt的都是第三的模块
#安装第三方模块 终端: pip install 模块名 PyCharm: stttings---project---加号(+)---模块名
improt xlrd
# 获取所有所有表格名称
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))


# 时间类型转换
# print(sheet.cell(6,5).value)
# print(xlrd.xldate_as_datetime(sheet.cell(6,5).value,1))

---------------------------------我是分割线------------------------------------------------

improt xlwt
work_book = xlrd.open_workbook('路径') #读取表格
work = xlwt.workbok() #创建工作簿对象
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")

原文地址:https://www.cnblogs.com/zhuqihui/p/10865133.html