zoukankan      html  css  js  c++  java
  • python--excel

    import xlrd, xlwt


    # 读取Excel
    def read_excel(excel_name, sheet_name):
    if excel_name and excel_name:
    all_data_list = []
    try:
    workbook = xlrd.open_workbook(excel_name)
    # sheets_name = workbook.sheet_names()
    sheet_data = workbook.sheet_by_name(sheet_name)
    rows = sheet_data.nrows
    cols = sheet_data.ncols
    #获取第一行数据
    first_row_data = sheet_data.row_values(0)
    for row_num in range(1,rows):
    row_data = sheet_data.row_values(row_num)
    dict_data = dict(zip(first_row_data, row_data))
    all_data_list.append(dict_data)
    return all_data_list
    except:
    print("文件名或表名有误!")
    else:
    print("文件名或表名有问题耶!")


    # 写入表格
    def write_excel(sheet_name, all_data_dict_list):
    if sheet_name and all_data_dict_list:
    handle = xlwt.Workbook()
    sheet_one = handle.add_sheet(sheet_name, cell_overwrite_ok=False)
    first_data = all_data_dict_list[0]
    key_list = list(first_data.keys())
    for i in range(0, len(key_list)):
    row_0 = sheet_one.row(0)
    row_0.write(i, key_list[i])
    handle.save('E:/' + sheet_name + '.xls')
    for i in range(1,len(all_data_dict_list)):
    pass

  • 相关阅读:
    Junit单元测试
    Stream流方法引用
    Stream流思想和常用方法
    算法
    函数式接口
    Zookeeper理解
    GreenPlum学习之(Share-nothing)架构
    链表反转问题
    KMP算法的java实现
    KMP详解之二
  • 原文地址:https://www.cnblogs.com/fqfanqi/p/8413478.html
Copyright © 2011-2022 走看看