!/usr/bin/python
coding=utf-8
import xlrd
import xlwt
def read_excel():
ExcelFile=xlrd.open_workbook(r'/Users/liyuanlong/WorkSpace/test_paper/original.xlsx')
sheet_name=ExcelFile.sheet_names()[0]
sheet=ExcelFile.sheet_by_index(0)
row_cnt = sheet.nrows
cols1=sheet.col_values(0)
cols2=sheet.col_values(1)
dictAll = {}
for i in range(row_cnt):
if dictAll.has_key(sheet.row_values(i)[0]):
dictAll[sheet.row_values(i)[0]]["cnt"] += 1
dictAll[sheet.row_values(i)[0]]["content"].append(sheet.row_values(i)[1])
else:
dictAll[sheet.row_values(i)[0]]={
"cnt":1,
"content":[sheet.row_values(i)[1]]
}
return dictAll
def write_excel(contentDict):
f = xlwt.Workbook()
sheet1 = f.add_sheet(u'sheet1',cell_overwrite_ok=True)
cnt = 0
for i in contentDict.keys():
oneLine = contentDict[i]
sheet1.write(cnt,0,i)
sheet1.write(cnt,1,oneLine["cnt"])
sheet1.write(cnt,2,oneLine["content"])
cnt += 1
f.save('processed.xls')
if name =='main':
content = read_excel()
write_excel(content)