pandas读写excel

【openpyxl】、【pandas】、【xlrd、xlwt】读写excel的区别

openpyxl读写excel

  https://www.cnblogs.com/hudieren/p/16792200.html

pandas读写excel

  https://www.cnblogs.com/hudieren/p/16792215.html

xlrd读excel文件、xlwt写入excel文件

  https://www.cnblogs.com/hudieren/p/16792234.html

建议使用openpyxl写入,使用xlrd读取,xlwt速度也快,不使用的原因是只可以生成.xls文件

 

# -*- coding: utf-8 -*-
# @Author  : 107
# @File    : pandas读写.py
# @explain :
import pandas as pd
import time


def read_xlsx():
    name = "test.xlsx"
    sheet1 = pd.read_excel(name, sheet_name="地区")
    # nrows, ncols = sheet1.shape  # 多少行,多少列
    for i in range(sheet1.shape[0]):
        data = [sheet1.iloc[i, j] for j in range(sheet1.shape[1])]
        print(data)

    sheet2 = pd.read_excel(name, sheet_name="")
    for i in range(sheet2.shape[0]):
        data = [sheet2.iloc[i, j] for j in range(sheet2.shape[1])]
        print(data)

    sheet3 = pd.read_excel(name, sheet_name="男名")
    for i in range(sheet3.shape[0]):
        data = [sheet3.iloc[i, j] for j in range(sheet3.shape[1])]
        print(data)

    sheet4 = pd.read_excel(name, sheet_name="女名")
    for i in range(sheet4.shape[0]):
        data = [sheet4.iloc[i, j] for j in range(sheet4.shape[1])]
        print(data)


def write_xlsx():
    pass


if __name__ == '__main__':
    start_time = int(time.time() * 1000)
    read_xlsx()  # 读取 xlsx文件:994 毫秒   读取xls文件:660毫秒
    # write_xlsx()
    end_time = int(time.time() * 1000)
    print(f"共花费 {end_time - start_time} 毫秒")

 

posted @ 2022-10-14 17:00  167  阅读(47)  评论(0)    收藏  举报