#读取xml内容到字典对象 defread_xls(filename, sheetname): #打开student.xls xls = xlrd.open_workbook(filename) #读取student表 sheet = xls.sheet_by_name(sheetname) data = {} # 获取每行内容,以row为键,后面的列表为值,放到字典 for n inrange(sheet.nrows): row_d = sheet.row_values(n) data[row_d[0]] = list(row_d[1:])
return data
#按照题目要求美化字符串 defpretty_str(dicts): text = "".join('{\n') for k insorted(dicts.keys()): lists = dicts[k] s = '\t\t\t"%s" : ["%s", %d, %d, %d],\n' % ( int(k), lists[0], int(lists[1]), int(lists[2]), int(lists[3])) text += s text += '\t\t}' text = text[::-1].replace(',', '', 1)[::-1] #处理列表最后一项后面的,