def fill_dict(fp): d = {} with open(fp) as entity_file: csv_reader = CSVUnicodeReader(entity_file) headers = csv_reader.next() for line in csv_reader: d[line[0]] = line[1] return d
# levels_fp = os.path.join(basedir, 'levels.csv') schools_fp = os.path.join(basedir, 'schools.csv') # subjects_fp = os.path.join(basedir, 'subjects.csv') def fill_dict(fp): d = {} with open(fp) as entity_file: csv_reader = CSVUnicodeReader(entity_file) headers = csv_reader.next() for line in csv_reader: d[line[0]] = line[1] return d # levels = fill_dict(levels_fp) schools = fill_dict(schools_fp) # subjects = fill_dict(subjects_fp) with open(item_filepath, mode='r') as item_f, open(item_cover, mode='w') as item_fw: item_r = CSVUnicodeReader(item_f) item_r.next() item_w = CSVUnicodeWriter(item_fw) for idx, course in enumerate(item_r): if idx > 4805: break course_id = course[0] course_school = course[7][1:] school_cover_image = schools[course_school] course[3] = school_cover_image item_w.writerow(course)
def fill_dict(fp): d = {} with open(fp) as entity_file: csv_reader = CSVUnicodeReader(entity_file) headers = csv_reader.next() for line in csv_reader: d[line[0]] = line[1:] return d levels = fill_dict(levels_fp) schools = fill_dict(schools_fp) subjects = fill_dict(subjects_fp) with open(item_filepath, mode='r') as item_f, open(item_uog_level, mode='w') as level_f, \ open(item_in_school, mode='w') as school_f, open(item_is_subject_of, mode='w') as subject_f: item_r = CSVUnicodeReader(item_f) level_w = CSVUnicodeWriter(level_f) school_w = CSVUnicodeWriter(school_f) subject_w = CSVUnicodeWriter(subject_f) item_r.next() # headers level_w.writerow(('uog_id', 'uog_id')) school_w.writerow(('uog_id', 'uog_id')) subject_w.writerow(('uog_id', 'wikidata_id')) for idx, course in enumerate(item_r): if idx > 4805: break course_id = course[0] course_level = course[6][1:] level_id = levels[course_level] level_w.writerow((course_id, level_id[0]))