python 通过xlrd库读取excel

2019-05-14 16:46:17 浏览数 (2)

python通过xlrd库读取excel一例

代码语言:javascript复制
#!/usr/bin/env python
# coding=utf-8
# author: wz
# mail: 277215243@qq.com
# datetime:2017/5/3 6:32 PM
# web: https://www.bthlt.com
import xlrd


def open_zones():
    open_zone_xls = xlrd.open_workbook("./openzone.xlsx")
    sheet = open_zone_xls.sheet_by_index(0)
    zone_id_index = 0
    zone_id_ip = 1
    zone_name_index = 2
    zone_attr_buf = 3
    zone_ip = 4
    zone_plat_index = 5
    zone_connect_url = 6
    zone_rs_ip_list = 7
    rows = sheet.nrows
    excel_list = list()
    for row in xrange(1, rows):
        row_dic = dict()
        row_dic['zone_id'] = int(sheet.cell_value(rowx=row, colx=zone_id_index))
        row_dic['zone_name'] = str(sheet.cell_value(rowx=row, colx=zone_name_index).encode("utf-8")).strip()
        row_dic['zone_plat'] = str(int(sheet.cell_value(rowx=row, colx=zone_plat_index))).strip()
        row_dic['zone_id_ip'] = str(sheet.cell_value(rowx=row, colx=zone_id_ip)).strip()
        row_dic['zone_attr_buf'] = str(sheet.cell_value(rowx=row, colx=zone_attr_buf)).strip()
        row_dic['zone_rs_ip_list'] = str(sheet.cell_value(rowx=row, colx=zone_rs_ip_list)).strip()
        row_dic['zone_ip'] = str(sheet.cell_value(rowx=row, colx=zone_ip)).strip()
        row_dic['zone_url'] = str(sheet.cell_value(rowx=row, colx=zone_connect_url)).strip()
        excel_list.append(row_dic)
    return excel_list


if __name__ == "__main__":
    print open_zones()

0 人点赞