''' 讀 excel文件 ''' def read_from_excel(filepath): data = xlrd.open_workbook(filepath) table = data.sheets()[0] nor = table.nrows nol = table.ncols print 'row: %d , colume: %d' % (nor, nol) resutl = [] for i in range(1, nor): dict = {} flag = True # if i == 10: # break for j in range(nol): title = table.cell_value(0, j).encode('utf-8') print(str(i) + '--' + str(j) + '---'+ title) #print(chardet.detect(table.cell_value(i, j))) value = (str(table.cell_value(i, j).encode('utf-8')).replace('\n', '')) print(str(i) + '--' + str(j) + '---'+value) # print value if title == 'identitu_type': if value == 'SSS': value = 'SSS card' elif value == 'PASSPORT': value = 'Passport' elif value == 'DRIVERLICENCE': value = "Driver's license" elif value == 'PHILHEALTH': value = "PhilHealth" elif value == 'UMID': value = "UMID" else: flag = False print(str(i) + '--' + str(j) + '---'+value) dict[title] = remove_emoji(value) if flag: resutl.append(dict) return resutl
''' 字典轉 csv文件 ''' def nestedlist2csv(list, out_file): with open(out_file, 'wb') as f: title = [] w = csv.writer(f) fieldnames=list[0].keys() # solve the problem to automatically write the header print fieldnames title = ['Name','id_card', 'phone','identitu_type','Date'] w.writerow(title) for row in list: print(row.values) value = [row['Name'], row['id_card'], row['phone'], row['identitu_type'], row['Date']] w.writerow(value)