Q:使用python翻译excel->字典

问题描述 投票:0回答:1

我想从Excel文件创建字典。例如,如果数据的组织方式如下:

x1 1 y1 0 f1 4
x2 2 y2 2 f2 1
x3 3 y3 8 f3 7

我想创建一个输出以下内容的字典:

[{'x1': '1', 'y1': '0', 'f1': '4'},{'x2':'2', 'y2':'2','f2':'1'},{'x3':'3','y3':'8','f3':'7'}]
python excel
1个回答
1
投票
import xlrd # Give the location of the file loc = ("YOURFILENAME.xls") # To open Workbook wb = xlrd.open_workbook(loc) sheet = wb.sheet_by_index(0) my_arr = [] for i in range(sheet.nrows): my_dict = {} my_dict[str(sheet.cell_value(i, 0))] = str(int(sheet.cell_value(i, 1))) my_dict[str(sheet.cell_value(i, 2))] = str(int(sheet.cell_value(i, 3))) my_dict[str(sheet.cell_value(i, 4))] = str(int(sheet.cell_value(i, 5))) my_arr.append(my_dict) print(my_arr)
© www.soinside.com 2019 - 2024. All rights reserved.