目录:
subprocess模块
#run方法为Popen的封装,效果相同
import subprocess
"""
order = subprocess.Popen('diroo',
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
res = order.stdout.read().decode('gbk')
print(res)
print('==============================')
res = order.stderr.read().decode('gbk')
print(res)
"""
order = subprocess.run('dir',
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
res = order.stdout.decode('gbk')
print(res)
print('==============================')
res = order.stderr.decode('gbk')
print(res)
configparser模块
import configparser
# 1.初始化
parser = configparser.ConfigParser()
# 2.读
parser.read('my.ini', encoding='utf-8')
# section | option | value
sts = parser.sections()
print(sts, type(sts)) # ['server', 'client'] <class 'list'>
ops = parser.options(sts[0])
print(ops) # ['ip', 'port']
value = parser.get(sts[0], ops[0])
print(value, type(value))
# get=>str getboolean=>bool getfloat=>float getint=>int 直接获得对应数据类型
print(parser.get('server', 'port'))
# 3.写
parser.read('my.ini', encoding='utf-8')
parser.set('server', 'port', '6666')
parser.write(open('my.ini', 'w'))
xml模块
import xml
# 用途:xml文件:1.作为传输文件用于数据的传输 2.作为配置文件配置信息
"""
1.只能由一个根标签
2.所有的标签都是自定义的
3.标签名就是key,标签的内容就是value
4.与json不同的是,标签不仅可以有key和value,还有标签的属性
注:xml的属性通常用来表示标签间的区分度,用于解析xml来使用
"""
"""
{"countrys": [
{
"rank": 2,
"year": 2008,
"gdppc": 141100
},
{},
{}
]}
{
"data": {
"countrys": [
{},
{},
{}
]
}
}
"""
# xml的文件解析 => 将xml转化为json类型的数据
# dict list => json
import xml.etree.ElementTree as ET
"""
# 读文件
tree = ET.parse("my.xml")
# print(tree) # xml.etree.ElementTree.ElementTree
# 根节点
root_ele = tree.getroot()
# print(root_ele) # Element 'data'
# 遍历往下
# print(root_ele[1])
for ele in root_ele:
print(ele, ele.attrib)
if ele.attrib['name'] == 'Singapore':
for e in ele:
print(e, e.tag)
if e.tag == 'gdppc':
print(e.text)
e.text = '88888' # 只修改了内容
# 全文搜索指定名的子标签
# ele.iter("标签名")
# 非全文查找满足条件的第一个子标签
# ele.find("标签名")
# 非全文查找满足条件的所有子标签
# ele.findall("标签名")
print('==============')
cs = root_ele.iter('country')
for c in cs:
print(c)
print('==============')
print(root_ele.find('country').attrib)
# print(root_ele.find('rank').attrib) # 不能跨标签取,只能取子标签
print('==============')
print(root_ele.findall('country'))
# 将内存的数据重新写入文件
tree.write("my.xml")
"""
"""
{
"countrys": [
{
"rank": 2,
"year": 2008,
"gdppc": 141100
}
]
}
"""
data = {'countrys': []}
tree = ET.parse("my.xml")
root = tree.getroot()
for ele in root:
country = {}
for e in ele:
if e.text and e.text.strip():
country[e.tag] = e.text
data['countrys'].append(country)
print(data)
excel模块
#注意模块从0开始,excel从1开始
import xlrd
"""
# 读取文件
work_book = xlrd.open_workbook("my.xlsx")
# 获取所有所有表格名称
print(work_book.sheet_names())
# 选取一个表
sheet = work_book.sheet_by_index(0)
# 表格名称
print(sheet.name)
# 行数
print(sheet.nrows)
# 列数
print(sheet.ncols)
# 某行全部
print(sheet.row(6))
print(sheet.row(13))
# 某列全部
print(sheet.col(4))
# 某行列区间
print(sheet.row_slice(6, start_colx=0, end_colx=4))
# 某列行区间
print(sheet.col_slice(3, start_rowx=0, end_rowx=3))
# 某行类型0:空 1:str 2:num 3:date | 值
print(sheet.row_types(1), sheet.row_values(6))
# 单元格
print(sheet.cell(6,0).value) # 取值
print(sheet.cell(6,0).ctype) # 取类型
print(sheet.cell_value(6,0)) # 直接取值
print(sheet.cell_type(6,0)) # 直接取类型
# 0:以1900年为基准 1:以1904年为基准
print(xlrd.xldate_as_datetime(sheet.cell(6, 0).value, 0))
"""
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']
# 写入标题
c = 0
for k in keys:
# sheet.write(0, keys.index(k), k, style)
# sheet.write(0, keys.index(k), k)
sheet.write(keys.index(k) + 5, 2, k if k != 'Egon' else 'cool')
# sheet.write(c, c, k)
# c += 1
# 写入数据
# sheet.write(1, 0, 'cool', style)
# sheet.write(0, 0, 'cool')
# 保存至文件
work.save("new_my.xls")