makeDictionary.py 文件源码

python
阅读 15 收藏 0 点赞 0 评论 0

项目:ipeds-scraper 作者: UrbanInstitute 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号