zoukankan      html  css  js  c++  java
  • day53——Python 处理 Excel 数据

    (Windows 下操作) 先安装一些处理 Excel 的模块:

    1 pip install xlrd        # 用于读取Excel数据
    2 pip install xlwt        # 用于写入Excel数据
    3 pip install xlutils     # 用于修改Excel数据

    Python 读取 Excel 数据:

     1 #!/usr/bin/env python
     2 #-*- coding:utf-8 -*-
     3 
     4 import xlrd
     5 
     6 def readExcel():
     7     data = xlrd.open_workbook('1.xlsx')    # 打开一个Excel表格
     8     table = data.sheets()[0]               # 打开Excel表格的第一张表
     9     nrows = table.nrows                    # 获取每张表的行数
    10     for line in range(nrows):              # 遍历每一行
    11         print(table.row_values(line))      # 获取每行的值
    12 
    13 if __name__ == "__main__":
    14     readExcel()
    ['姓名', '性别', '年龄']
    ['小明', '', 17.0]
    ['小红', '', 18.0]
    ['小李', '', 19.0]
    ['小张', '', 20.0]

    Python 读取某一列数据:

     1 #!/usr/bin/env python
     2 #-*- coding:utf-8 -*-
     3 
     4 import xlrd
     5 
     6 def readExcel():
     7     data = xlrd.open_workbook('1.xlsx')    # 打开一个Excel表格
     8     table = data.sheets()[1]               # 打开excel表格的第一张表
     9     ncols = table.ncols                    # 获取每张表的列数
    10     for col in range(ncols):               # 遍历每一列
    11         print(table.col_values(col)[0])    # 获取第一列的值
    12 
    13 if __name__ == "__main__":
    14     readExcel()

    Python 创建 Excel 表:

     1 #!/usr/bin/env python
     2 #-*- coding:utf-8 -*-
     3 
     4 import xlwt
     5 
     6 excel = xlwt.Workbook()               # 创建一个Excel文件
     7 sheet1 = excel.add_sheet("sheet1")    # 添加一张表,表名为"sheet1"
     8 sheet1.write(0, 0, "Name")            # 表示在第一行第一列写入内容"Name"
     9 sheet1.write(1, 0, "John")            # 表示在第二行第一列写入内容"John"
    10 sheet1.write(2, 0, "Jeny")            # 表示在第三行第一列写入内容"Jeny"
    11 excel.save("1.xls")                   # 保存Excel文件,并命名为"1.xls"

    Python 读取某一列数据:

     1 #!/usr/bin/env python
     2 #-*- coding:utf-8 -*-
     3 
     4 import xlrd
     5 
     6 def readExcel():
     7     data = xlrd.open_workbook('1.xlsx')    # 打开一个Excel表格
     8     table = data.sheets()[1]               # 打开excel表格的第一张表
     9     ncols = table.ncols                    # 获取每张表的列数
    10     for col in range(ncols):               # 遍历每一列
    11         print(table.col_values(col)[0])    # 获取第一列的值
    12 
    13 if __name__ == "__main__":
    14     readExcel()

    Python 创建 Excel 表:

     1 #!/usr/bin/env python
     2 #-*- coding:utf-8 -*-
     3 
     4 import xlwt
     5 
     6 excel = xlwt.Workbook()               # 创建一个Excel文件
     7 sheet1 = excel.add_sheet("sheet1")    # 添加一张表,表名为"sheet1"
     8 sheet1.write(0, 0, "Name")            # 表示在第一行第一列写入内容"Name"
     9 sheet1.write(1, 0, "John")            # 表示在第二行第一列写入内容"John"
    10 sheet1.write(2, 0, "Jeny")            # 表示在第三行第一列写入内容"Jeny"
    11 excel.save("1.xls")                   # 保存Excel文件,并命名为"1.xls"

    Python 修改 Excel 数据:

     1 #!/usr/bin/env python
     2 #-*- coding:utf-8 -*-
     3 
     4 import xlrd
     5 import xlutils.copy
     6 
     7 data = xlrd.open_workbook('1.xls')    # 打开一个Excel文件
     8 excel = xlutils.copy.copy(data)       # 相当于复制Excel文件,然后在复制的文件上操作
     9 sheet1 = excel.get_sheet(0)           # 获取要修改的表(这里我修改Excel文件的第一张表)
    10 sheet1.write(2, 0, 'Tom')             # 表示把第三行第一列的数据修改为'Tom'
    11 excel.save('1.xls')                   # 保存Excel文件
  • 相关阅读:
    ASP.NET Core路由中间件[4]: EndpointRoutingMiddleware和EndpointMiddleware
    ASP.NET Core路由中间件[3]: 终结点(Endpoint)
    ASP.NET Core路由中间件[2]: 路由模式
    ASP.NET Core路由中间件[1]: 终结点与URL的映射
    [LeetCode] 994. Rotting Oranges 腐烂的橘子
    [LeetCode] 993. Cousins in Binary Tree 二叉树的表兄弟节点
    [LeetCode] 992. Subarrays with K Different Integers 有K个不同整数的子数组
    [LeetCode] 991. Broken Calculator 损坏的计算器
    [LeetCode] 1143. Longest Common Subsequence 最长公共子序列
    [LeetCode] 990. Satisfiability of Equality Equations 等式方程的可满足性
  • 原文地址:https://www.cnblogs.com/yangjinbiao/p/8310119.html
Copyright © 2011-2022 走看看