Last active
August 3, 2018 20:51
-
-
Save dannguyen/d83f27a93b2e6f80edda22cfa0f0a1d6 to your computer and use it in GitHub Desktop.
Revisions
-
dannguyen revised this gist
Apr 12, 2016 . 2 changed files with 28 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ """ Same as the other script, but attempts to preserve the same folder structure in `csv` as it exists in `xlsx` """ from csv import writer from glob import glob from os.path import basename, dirname, join from os import makedirs from xlrd import open_workbook import re CSV_DIRNAME = 'csv' XLS_DIRNAME = 'xlsx' for xfname in glob(join(XLS_DIRNAME, '*', '*.xlsx')): cdir = re.sub(r'^' + XLS_DIRNAME, CSV_DIRNAME, dirname(xfname)) makedirs(cdir, exist_ok=True) cname = join(cdir, basename(xfname) + '.csv') print("writing to", cname) cf = open(cname, 'w') cv = writer(cf) book = open_workbook(xfname) sheet = book.sheets()[0] for n in range(sheet.nrows): rowvals = sheet.row_values(n) cv.writerow(rowvals) cf.close() File renamed without changes. -
dannguyen revised this gist
Apr 12, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ """ This code processes each xlsx file in the xlsx/* path, then writes the data (a list of values) into a single CSV folder (i.e. flat structure) """ from csv import writer -
dannguyen created this gist
Apr 12, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ """ This code processes each xlsx file in the xlsx/* path, then writes the data (a list of values) to CSV files stored in the csv/ path """ from csv import writer from glob import glob from os.path import basename, join from os import makedirs from xlrd import open_workbook import re CSV_DIRNAME = 'csv' makedirs(CSV_DIRNAME, exist_ok=True) for xfname in glob(join('xlsx', '*', '*.xlsx')): cname = join(CSV_DIRNAME, basename(xfname) + '.csv') print("writing to", cname) cf = open(cname, 'w') cv = writer(cf) book = open_workbook(xfname) sheet = book.sheets()[0] for n in range(sheet.nrows): rowvals = sheet.row_values(n) cv.writerow(rowvals) cf.close()