Created
September 6, 2015 21:08
-
-
Save mkowoods/70d7ecc77ede7bd1bbbf to your computer and use it in GitHub Desktop.
Simple Example Using xlrd to open an xlsx file run a basic script and write the results to a csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xlrd | |
import csv, codecs, cStringIO | |
path = "Book1.xlsx" | |
wb = xlrd.open_workbook(path) | |
sheet1 = wb.sheet_by_index(0) | |
rows = sheet1.get_rows() | |
rows.next() #skip header row | |
def _handle_conversions(val): | |
if type(val) == unicode: | |
return val | |
elif type(val) == float: | |
return unicode(int(val)) | |
else: | |
return unicode(val) | |
i = 0 | |
data = [] | |
for row in rows: | |
i += 1 | |
internal_sku = _handle_conversions(row[1].value) | |
oem_part = _handle_conversions(row[2].value) | |
descr = _handle_conversions(row[3].value) | |
if ';' in oem_part: | |
oem_components = oem_part.split(";") | |
for comp in oem_components: | |
if comp: | |
data.append([internal_sku, comp, descr]) | |
else: | |
data.append([internal_sku, oem_part, descr]) | |
#From CSV Module Doumentation | |
class UTF8Recoder: | |
def __init__(self, f, encoding): | |
self.reader = codecs.getreader(encoding)(f) | |
def __iter__(self): | |
return self | |
def next(self): | |
return self.reader.next().encode("utf-8") | |
class UnicodeWriter: | |
def __init__(self, f, dialect=csv.excel, encoding="utf-8-sig", **kwds): | |
self.queue = cStringIO.StringIO() | |
self.writer = csv.writer(self.queue, dialect=dialect, **kwds) | |
self.stream = f | |
self.encoder = codecs.getincrementalencoder(encoding)() | |
def writerow(self, row): | |
'''writerow(unicode) -> None | |
This function takes a Unicode string and encodes it to the output. | |
''' | |
self.writer.writerow([s.encode("utf-8") for s in row]) | |
data = self.queue.getvalue() | |
data = data.decode("utf-8") | |
data = self.encoder.encode(data) | |
self.stream.write(data) | |
self.queue.truncate(0) | |
def writerows(self, rows): | |
for row in rows: | |
self.writerow(row) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment