day22,ConfigParser,subprocess,xlrd三个模块

本文介绍了使用Python的ConfigParser模块来管理和操作配置文件的方法,包括读取、修改和保存配置信息。同时,还讲解了如何利用subprocess模块执行Shell命令,并展示了如何使用xlrd模块读取Excel文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                                  今日所学的内容    

ConfigParser

    最外层的就是ConfigParser对象,它是由多个的Section块组成。

Section
    这是ConfigParser中的配置块,可以通过Section标识功能或范围一致的配置信息。

    乱分块或是不分块也没问题,主要的是键值对的配置信息的获取。

    通过块区分,相同的option名称也可以进行区分,好比名称空间。

    但是至少要知道信息在哪。

Option
    这个就是单个的键值对,属性名=属性值,很容易区分。

完整的配置文件如图

 

读取

读取顺序

    读取配置时,讲究先section才能获取option。

    ConfigParser对象相当于一个字典,外一层是section,内层就是option,所以在操作上能提供很大便

保存

 

首先获取 到 

c = configparser.ConfigParser
这个对象,然后需要读出来自己需要的是哪个文件
c.read("wuser.cfg",encoding="utf-8")
然后在设置值到配置文件中
然后在最后需要写进文件中
with open("wuser.cfg","w",encoding="utf-8") as wf:
c.write(wf)

subprocess模块:操作shell命令

import subprocess
order = subprocess.Popen('终端命令', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
suc_res = order.stdout.read().decode('系统默认编码')
err_res = order.stderr.read().decode('系统默认编码')

order = subprocess.run('终端命令', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
suc_res = order.stdout.decode('系统默认编码')
err_res = order.stderr.decode('系统默认编码')

xlrd模块: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))

 



 

转载于:https://www.cnblogs.com/WBaiC1/p/10865008.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值