def makeMasterDict(start, stop):
print("*****************************")
print("Assembling master dictionary")
print("*****************************")
# Set up dictionary CSV
with open('data/dictionary.csv', 'w') as f:
c = csv.writer(f)
c.writerow(['year', 'dictname', 'dictfile', 'varnumber', 'varname', 'datatype' ,'fieldwidth', 'format', 'imputationvar', 'vartitle'])
f.close()
# For each Excel dictionary, take the contents and file name and add to master dictionary csv
for i in range(start,stop):
for file in os.listdir('dict/' + str(i) + '/'):
if file.endswith((".xls", ".xlsx")):
print("Adding " + str(i) + " " + file + " to dictionary")
dictname = file.split(".", 1)[0]
rowstart = [i, dictname, file]
workbook = xlrd.open_workbook('dict/' + str(i) +'/' + file, on_demand = True)
worksheet = workbook.sheet_by_name('varlist')
with open('data/dictionary.csv', 'a') as f:
c = csv.writer(f)
for r in range(2,worksheet.nrows):
varrow = worksheet.row_values(r)
row = rowstart + varrow
c.writerow(row)
评论列表
文章目录