- 论坛徽章:
- 0
|
本帖最后由 GhostFromHeaven 于 2013-05-24 01:04 编辑
回复 1# su8610
参考:http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html- #-*- coding:utf-8 -*-
- import xlrd
- if __name__ == "__main__":
- book = xlrd.open_workbook("myfile.xls")
- #print "The number of worksheets is", book.nsheets
- #print "Worksheet name(s):", book.sheet_names()
- sheet = book.sheet_by_index(0)
- #print sheet.name, sheet.nrows, sheet.ncols
- row_count = 4 #sheet.nrows
- col_count = 5 #sheet.ncols
-
- belongings_map = {}
- for irow in range(1, row_count):
- belongings_dict = {}
- name = sheet.cell_value(rowx=irow, colx=0)
- for icol in range(1, col_count):
- amount = sheet.cell_value(rowx=irow, colx=icol)
- if amount:
- belongings = sheet.cell_value(rowx=0, colx=icol)
- belongings_dict[belongings] = int(float(amount))
- belongings_map[name] = belongings_dict
-
- print "所有物品:\n", belongings_map
-
- #打印C的所有物品:
- name = "C"
- print "\n%s拥有的物品:" % name
- for (belongings, amount) in belongings_map[name].items():
- print belongings, amount
-
复制代码
|
|