import bs4, os, re, csv pattern = r'.+\.xml$' folder_name = 'spiskovi' path = os.path.join( '.', folder_name ) os.chdir( path ) files = os.listdir() if files is []: print("Folder je prazan!") exit() regex = re.compile( pattern ) groups = [] groups_info = [] files.sort() for file in files: reObj = regex.search( file ) if reObj is not None: groups += [ reObj.group() ] for group_name in groups: group_file = open( group_name, 'r' ) group = bs4.BeautifulSoup( group_file, features="html.parser" ) group_file.close() students = group.select("student") if students == []: continue studenti_info = dict() for student in students: ime = student.find( "ime" ).get_text() prezime = student.find( "prezime" ).get_text() ocene =student.find("ocene") lista_ocena = ocene.select('ocena') broj_ocena = len( lista_ocena ) prosek = -1 if broj_ocena is not 0: lista_ocena = list( map( lambda x: int(x.get_text()), lista_ocena ) ) prosek = sum( lista_ocena ) / broj_ocena indeks = student.find( "indeks" ).get_text() if studenti_info.get( indeks ) == None: studenti_info[ indeks ] = ( ime, prezime, prosek, group_name[:-4] ) else: print("Student sa indeksom %s je vec unet!" % indeks ) groups_info += [ studenti_info ] polja = [ 'Ime', 'Prezime', 'Indeks', 'Prosek', 'Grupa' ] najbolji_file = open('Pera.csv', 'w', newline = '' ) najbolji_writer = csv.writer( najbolji_file ) najbolji_writer.writerow( polja ) for group_info in groups_info: items = list( group_info.items() ) items.sort( key = lambda x: x[1][2]) najbolji = list( items[-1][1][:2] ) + [ items[-1][0] ] + list( items[-1][1][2:] ) najbolji_writer.writerow( najbolji ) najbolji_file.close() os.chdir('..')