Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! python3
- """
- XLSX-SCHEDULE MAKER
- This program is processes schedule text from txt file and puts it into xlsx file
- MODULES USED: re, openpyxl, datetime
- PLAN:
- step1 - read schedule from the file
- step2 - analyze schedule with re and datetime, find the first day date
- step3 - create data structure Schedule to easily use it later
- step4 - import data into xlsx file
- """
- from pprint import pprint as pp
- import re
- import datetime
- import Schedule
- # import openpyxl
- def import_from_file(directory):
- """
- function reads file and assigns its contents to variable schedule
- :param directory: str
- :return schedule: str
- """
- txt = open(directory, "r")
- schedule = txt.read()
- txt.close()
- return schedule
- def extract_info(data):
- # splitting schedule into sections (0 - general info, days: 1-5)
- structure = re.split('-{2,}\n', data)
- schedule_info = get_header_info(structure[0])
- create_structure(schedule_info)
- n = split_to_periods(structure[2])
- # list of disciplines
- # d = re.split('\n\* ', n[1])
- # k = re.split(r'\n(?=\d пара)', n[2])
- # print(k)
- def get_header_info(data):
- """
- function extracts data from header using regex module
- :param data: text from header
- :return tuple: group, year, semester, start_date
- """
- group = re.search(re.compile(r'(?<=Група )\w{2,3}(-\d{2})*'), data).group()
- year = re.search(re.compile(r'\d{4}-\d{4} н.р.'), data).group()
- semester = 1 if re.search(re.compile(r'\w+(?= семестр)'), data).group() is 'І' else 2
- first_monday = re.search(re.compile(r'\d{2}.\d{2} '), data).group()[:-1]
- start_date = datetime.date(int(year[0:4]) if semester == 1 else int(year[5:9]),
- int(first_monday[3:]), int(first_monday[:2]))
- print('Extracting header data...\ngroup: {}\nyear: {}\nsemester: {}\nfirst week monday date: {}'
- .format(group, year, semester, start_date))
- return group, year, semester, start_date
- def split_to_periods(day):
- # function for splitting weekday into periods
- return re.split('\n(?=\d пара)', day)
- def get_disciplines(period):
- """
- :param period: text containing info about all disciplines of this period
- :return: list with information about each discipline
- """
- def create_structure(semester_info):
- schedule = Schedule.Schedule # creating object Schedule
- # adding info
- schedule.group = semester_info[0]
- schedule.year = semester_info[1]
- schedule.semester = semester_info[2]
- schedule.start = semester_info[3]
- cont = schedule.weeks
- # week dates
- week_shift = datetime.timedelta(days=7)
- curr_date = schedule.start
- for i in range(17):
- curr_w = cont[i]
- for j in range(5):
- curr_w
- return schedule
- """
- Program
- """
- direct = "GІР.txt"
- dat = import_from_file(direct)
- extract_info(dat)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement