我想从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'}]
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)