python程序设计 第三版 中文高清pdf版 (John Zelle) 下载
python¶ÁÈ¡Excel±í¸ñÎļþ£¬ÀýÈç»ñÈ¡Õâ¸öÎļþµÄÊý¾Ý
python¶ÁÈ¡Excel±í¸ñÎļþ£¬ÐèÒªÈçϲ½Ö裺
1¡¢°²×°Excel¶ÁÈ¡Êý¾ÝµÄ¿â-----xlrd
Ö±½Ópip install xlrd°²×°xlrd¿â
#ÒýÈëExcel¿âµÄxlrd import xlrd
2¡¢»ñÈ¡ExcelÎļþµÄλÖò¢ÇÒ¶ÁÈ¡½øÀ´
#µ¼ÈëÐèÒª¶ÁÈ¡Excel±í¸ñµÄ·¾¶ data = xlrd.open_workbook(r'C:\Users\NHT\Desktop\Data\\test1.xlsx') table = data.sheets()[0]
3¡¢¶ÁÈ¡Ö¸¶¨µÄÐкÍÁеÄÄÚÈÝ£¬²¢½«ÄÚÈÝ´æ´¢ÔÚÁбíÖУ¨½«µÚÈýÁеÄʱ¼ä¸ñʽת»»£©
#´´½¨Ò»¸ö¿ÕÁÐ±í£¬´æ´¢ExcelµÄÊý¾Ý tables = [] #½«excel±í¸ñÄÚÈݵ¼Èëµ½tablesÁбíÖÐ def import_excel(excel): for rown in range(excel.nrows): array = {'road_name':'','bus_plate':'','timeline':'','road_type':'','site':''} array['road_name'] = table.cell_value(rown,0) array['bus_plate'] = table.cell_value(rown,1) #½«Excel±í¸ñÖеÄʱ¼ä¸ñʽת»¯ if table.cell(rown,2).ctype == 3: date = xldate_as_tuple(table.cell(rown,2).value,0) array['timeline'] = datetime.datetime(*date) array['road_type'] = table.cell_value(rown,3) array['site'] = table.cell_value(rown,4) tables.append(array)
4¡¢ÔËÐгÌÐò
if __name__ == '__main__': #½«excel±í¸ñµÄÄÚÈݵ¼Èëµ½ÁбíÖÐ import_excel(table) #ÑéÖ¤ExcelÎļþ´æ´¢µ½ÁбíÖеÄÊý¾Ý for i in tables: print(i)
5¡¢×îÖÕµÄÔËÐÐЧ¹ûÈçÏ£º
6¡¢ÍêÕûµÄ³ÌÐò´úÂ룺
import xlrd from xlrd import xldate_as_tuple import datetime #µ¼ÈëÐèÒª¶ÁÈ¡µÄµÚÒ»¸öExcel±í¸ñµÄ·¾¶ data1 = xlrd.open_workbook(r'C:\Users\NHT\Desktop\Data\\test.xlsx') table = data1.sheets()[0] #´´½¨Ò»¸ö¿ÕÁÐ±í£¬´æ´¢ExcelµÄÊý¾Ý tables = [] #½«excel±í¸ñÄÚÈݵ¼Èëµ½tablesÁбíÖÐ def import_excel(excel): for rown in range(excel.nrows): array = {'road_name':'','bus_plate':'','timeline':'','road_type':'','site':''} array['road_name'] = table.cell_value(rown,0) array['bus_plate'] = table.cell_value(rown,1) if table.cell(rown,2).ctype == 3: date = xldate_as_tuple(table.cell(rown,2).value,0) array['timeline'] = datetime.datetime(*date) array['road_type'] = table.cell_value(rown,3) array['site'] = table.cell_value(rown,4) tables.append(array) if __name__ == '__main__': #½«excel±í¸ñµÄÄÚÈݵ¼Èëµ½ÁбíÖÐ import_excel(table) for i in tables: print(i)
×ܽá
ÒÔÉÏËùÊöÊÇС±à¸ø´ó¼Ò½éÉܵÄpython¶ÁÈ¡Excel±í¸ñÎļþµÄ·½·¨,Ï£Íû¶Ô´ó¼ÒÓÐËù°ïÖú£¬Èç¹û´ó¼ÒÓÐÈκÎÒÉÎÊÇë¸øÎÒÁôÑÔ£¬Ð¡±à»á¼°Ê±»Ø¸´´ó¼ÒµÄ¡£ÔÚ´ËÒ²·Ç³£¸Ðл´ó¼Ò¶Ô½Å±¾Ö®¼ÒÍøÕ¾µÄÖ§³Ö£¡
Èç¹ûÄã¾õµÃ±¾ÎĶÔÄãÓаïÖú£¬»¶ÓתÔØ£¬·³Çë×¢Ã÷³ö´¦£¬Ð»Ð»£¡
转载请注明:谷谷点程序 » python读取Excel表格文件的方法