Skip to content

Instantly share code, notes, and snippets.

@denkiwakame
Last active August 29, 2015 14:23
Show Gist options
  • Save denkiwakame/f3920915744e08eca154 to your computer and use it in GitHub Desktop.
Save denkiwakame/f3920915744e08eca154 to your computer and use it in GitHub Desktop.
xlrd
from types import *
import xlrd
def value2str(val,encoding='cp932'): # or utf-8
if type(val) == FloatType: return str(val)
if type(val) == UnicodeType: return val.encode(encoding)
if type(val) == StringType: return val
raise TypeError("cannot convert '%s' type:%s to string" % (val,type(val)))
def xlrdrow2str(xlrd_row, delim=","):
return delim.join([ value2str(cell.value) for cell in xlrd_row ])
xlwoorkbook = xlrd.open_workbook('sample.xlsx')
xlsheet = xlwoorkbook.sheet_by_index(0)
nrow = xlsheet.nrows
for row_idx in range(nrow):
row = xlsheet.row(row_idx)
print xlrdrow2str(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment