Skip to content

Instantly share code, notes, and snippets.

@bitristan
Last active August 29, 2015 14:05
Show Gist options
  • Save bitristan/444c749d27d17b61fd5f to your computer and use it in GitHub Desktop.
Save bitristan/444c749d27d17b61fd5f to your computer and use it in GitHub Desktop.
use python to read and write xls
import xlrd
import xlwt
data = xlrd.open_workbook('input.xls')
table = data.sheets()[0]
nrows = table.nrows
ncols = table.ncols
print data.nsheets
print nrows, ncols
dict = {}
for i in range(nrows):
for j in range(ncols):
content = table.cell_value(i, j)
if not isinstance(content, float) and not isinstance(content, int):
content = content
//print content.encode('utf-8')
if content in dict:
dict[content] += 1
else:
dict[content] = 1
result = sorted(dict.items(), lambda x, y: cmp(x[1], y[1]), reverse=True)
f = xlwt.Workbook()
table = f.add_sheet('Sheet1')
for i in range(len(result)):
table.write(i, 0, result[i][0])
table.write(i, 1, result[i][1])
f.save('output.xls')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment