INFA_LAB4/5.py

31 lines
995 B
Python

import re
lessons = []
row = {}
columns = ['subject', 'weekday', 'time', 'group', 'audience', 'teacher', 'type', 'format']
header = 'Предмет,День недели,Время,Группа,Аудитория,Учитель,Тип урока,Формат урока'
with open('input.yaml', encoding='UTF-8') as f:
for i in f.readlines()[1:]:
if i.strip()[0] == '-':
if row:
lessons.append(row)
row = {}
i = i.split(':')
i[1] = ':'.join(i[1:])
i[0] = i[0].replace('-', '').strip()
i[1] = i[1].replace('"', '""').strip()
if any(map(lambda x: x in i[1], ['"', ',', "\\n"])):
i[1] = f'"{i[1]}"'
row[i[0]] = i[1]
lessons.append(row)
output = header + '\n'
with open('output.csv', 'w', encoding='UTF-8') as f:
for i in lessons:
for key in columns:
output += i.get(key, '') + ','
output = output[:-1] + '\n'
f.write(output)
print(output)