Last active
February 26, 2020 22:36
-
-
Save LBlend/56444624229221f6999d9f7630bf753a to your computer and use it in GitHub Desktop.
Some spaghetti code I threw together in order to sort survey data for a school project in sociology class.
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
""" | |
Some spaghetti code I threw together in order to sort | |
survey data for a school project. | |
""" | |
import xlrd | |
import xlwt | |
excel_file = xlrd.open_workbook(filename=f'input/raw_data.xlsx') | |
excel_sheet = excel_file.sheet_by_index(0) | |
wb = xlwt.Workbook() | |
def sorter(sheetname, rownumber): | |
drinkers = 0 | |
non_drinkers = 0 | |
sheet = wb.add_sheet(sheetname) | |
for row in range(excel_sheet.nrows): | |
x = excel_sheet.cell_value(row, rownumber) | |
if x != sheetname: | |
continue | |
drinkstatus = excel_sheet.cell_value(row, 3) | |
if drinkstatus == 'Ja': | |
drinkers += 1 | |
elif drinkstatus == 'Nei': | |
non_drinkers += 1 | |
sheet.write(0, 0, 'drikker') | |
sheet.write(0, 1, drinkers) | |
sheet.write(1, 0, 'drikker ikke') | |
sheet.write(1, 1, non_drinkers) | |
wb.save('output/sorted.xlsx') | |
sorter('Gutt', 1) | |
sorter('Jente', 1) | |
sorter('VG1', 2) | |
sorter('VG2', 2) | |
sorter('VG3', 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment