美文网首页
python3 中有关excel表的数据处理方法总结

python3 中有关excel表的数据处理方法总结

作者: gz_tester | 来源:发表于2018-04-10 17:27 被阅读0次

一、python处理excel数据,需要安装的包

包名 介绍 安装方式 官方文档
openpyxl openpyxl支持操作xlsx文件,不能操作xls文件;xlwt、wlrd只能读写xls文件,而不能操作xlsx文件 pip3 install openpyxl https://pypi.python.org/pypi/openpyxl
numpy 处理多维数组 pycharm中的安装方法:<请查看文档> https://pypi.python.org/pypi/numpy

二、操作excel表中数据的方法总结

1、有关Sheet表

a、设置要操作的sheet表,并读取指定的行或列

# 打开文件,filename=文件地址
file = openpyxl.load_workbook(filename)

# 查看该表中的所有sheet
sheetnames = file.sheetnames

# 打开指定的sheet,name=sheet的名称
sheet = file.get_sheet_by_name(name)

# 获取该sheet表中的第四列,第三行的数据
fourth_column = sheet["D"]
third_row = sheet["3"]

b、其他资料链接

Python操作Excel表格(openpyxl):http://blog.topspeedsnail.com/archives/5404

2、有关数据处理

a、合并两个列表中不同的元素

        # data001:[ ['波罗', 12, 23, 0.6], ['丘利', 19, 11, 0.5]]
        # data002:[['丘利', 0.1, 34, 51],['波罗', 0.15, 48, 74]]

        # 遍历返回数据表中的每一个元素,把事业部名称一致的,加到同一个列表中
        m = []
        for i in data001:
            for s in data002:
                if i[0] == s[0]:
                    m.append(i + s)

        # 去除列表中的循环数据
        excel_001_data = []
        for i in m:
            r = sorted(set(i), key=i.index)
            excel_001_data.append(r)

b、计算列表中两个数相除

        # data001 = [['波罗', 62, 3], ['丘利', 2, 1],["花花", 0, 0]]
        s = []
        for i in data001:
            s.append([i[0], i[1]/i[2] if i[2] > 0 else 0])

相关文章

网友评论

      本文标题:python3 中有关excel表的数据处理方法总结

      本文链接:https://www.haomeiwen.com/subject/hezghftx.html