S
S
sorra2021-06-01 15:16:44
Python
sorra, 2021-06-01 15:16:44

How to generate a dictionary from an Excel file using Python?

I am writing a program that will parse an unstructured Excel file and form a data dictionary of the following form:

dict = {"Курс 1":{
     "Semester-1":{"Предмет":{
        "maximum": 20,
        "solo": 20,
        "consult": 10,
        "obyaz": 10,
        "lessons": 10,
        "practic": 10,
        "lab": 10,
        "sem": 10,
        "cursovaya": 10,
        "project" : 10, 
    },
     "Недели":21
    },

    "Semester-2":{"Предмет":{
        "maximum": 20,
        "solo": 20,
        "consult": 10,
        "obyaz": 10,
        "lessons": 10,
        "practic": 10,
        "lab": 10,
        "sem": 10,
        "cursovaya": 10,
        "project" : 10, 
    },
        "Недели":21
    },
},
    "Курс 2":{
     "Semester-3":{"Предмет":{
        "maximum": 20,
        "solo": 20,
        "consult": 10,
        "obyaz": 10,
        "lessons": 10,
        "practic": 10,
        "lab": 10,
        "sem": 10,
        "cursovaya": 10,
        "project" : 10, 
    },
     "Недели":21
    },

    "Semester-4":{"Предмет":{
        "maximum": 20,
        "solo": 20,
        "consult": 10,
        "obyaz": 10,
        "lessons": 10,
        "practic": 10,
        "lab": 10,
        "sem": 10,
        "cursovaya": 10,
        "project" : 10, 
    },
        "Недели":21
    },
}}

I ran into such a problem that I don’t quite understand how to form a dictionary of this kind, I will need this dictionary in the future to insert values ​​into the Django model.

I partially collected the data myself, but I don’t quite understand how to write these values ​​\u200b\u200binto the dictionary in key-value pairs
import xlrd 
excel_data_file = xlrd.open_workbook('excel/god_nabora.xls')
sheet = excel_data_file.sheet_by_index(0)
row_number = sheet.nrows
main_subjects = []
main_course = []
main_semesters = []
main_week = []
main_hours_name = []
main_hours = []
if row_number > 0:
    # Предметы
    for row in range(20, 27):
        main_subjects.append(str(sheet.row(row)[2]).replace("text:","").replace("'","").replace(":"," ").replace("."," ").replace("empty","пустой"))
    # Курсы
    for row in range(20,100):
        key = (str(sheet.row(1)[row]).replace("text:","").replace("'","").replace(":"," ").replace("."," ").replace("empty","пустой"))
        if key != 'пустой ':
            main_course.append(key)
    # Семестры
    for row in range(20,100):
        key = (str(sheet.row(2)[row]).replace("text:","").replace("'","").replace(":"," ").replace("."," ").replace("empty","пустой"))
        if key != 'пустой ':
            main_semesters.append(key)
    # Количество недель
    for row in range(20,100):
        key = (str(sheet.row(3)[row]).replace("text:","").replace("'","").replace(":"," ").replace("."," ").replace("empty","пустой"))
        if key != 'пустой ':
            main_week.append(key)
    # За что часы
    for row in range(20,30):
        key = (str(sheet.row(4)[row]).replace("text:","").replace("'","").replace(":"," ").replace("."," ").replace("empty","пустой"))
        if key != 'пустой ':
            main_hours_name.append(key)
    # сами часы
    for row in range(20,100):
        key = (str(sheet.row(20)[row]).replace("text:","").replace("'","").replace(":"," ").replace("."," ").replace("empty","").replace("number", ""))
        if key != ' ':
            main_hours.append(key)


print("Список семестров - ",main_semesters)
print("Список курсов - ", main_course)
print("Недель в семестре - ", main_week)
print("Название нагрузки часы в одном семестре - ", main_hours_name)
print("Какие часы в одном семестре - ", main_hours)

Screenshot from the Excel file that was parsed
60b62a705badc132383878.jpeg

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question