diff --git a/TPC4/alunos2.csv b/TPC4/alunos2.csv new file mode 100644 index 0000000..85a417e --- /dev/null +++ b/TPC4/alunos2.csv @@ -0,0 +1,4 @@ +Número,Nome,Curso,Notas{5},,,,, +3162,Cândido Faísca,Teatro,12,13,14,15,16 +7777,Cristiano Ronaldo,Desporto,17,12,20,11,12 +264,Marcelo Sousa,Ciência Política,18,19,19,20,18 diff --git a/TPC4/alunos2.json b/TPC4/alunos2.json new file mode 100644 index 0000000..60f02fa --- /dev/null +++ b/TPC4/alunos2.json @@ -0,0 +1,38 @@ +[ + { + "Número": "3162", + "Nome": "Cândido Faísca", + "Curso": "Teatro", + "Notas": [ + 12, + 13, + 14, + 15, + 16 + ] + }, + { + "Número": "7777", + "Nome": "Cristiano Ronaldo", + "Curso": "Desporto", + "Notas": [ + 17, + 12, + 20, + 11, + 12 + ] + }, + { + "Número": "264", + "Nome": "Marcelo Sousa", + "Curso": "Ciência Política", + "Notas": [ + 18, + 19, + 19, + 20, + 18 + ] + } +] \ No newline at end of file diff --git a/TPC4/alunos3.csv b/TPC4/alunos3.csv new file mode 100644 index 0000000..cdcc571 --- /dev/null +++ b/TPC4/alunos3.csv @@ -0,0 +1,4 @@ +Número,Nome,Curso,Notas{3,5},,,,, +3162,Cândido Faísca,Teatro,12,13,14,, +7777,Cristiano Ronaldo,Desporto,17,12,20,11,12 +264,Marcelo Sousa,Ciência Política,18,19,19,20, diff --git a/TPC4/alunos3.json b/TPC4/alunos3.json new file mode 100644 index 0000000..f5f9577 --- /dev/null +++ b/TPC4/alunos3.json @@ -0,0 +1,35 @@ +[ + { + "Número": "3162", + "Nome": "Cândido Faísca", + "Curso": "Teatro", + "Notas": [ + 12, + 13, + 14 + ] + }, + { + "Número": "7777", + "Nome": "Cristiano Ronaldo", + "Curso": "Desporto", + "Notas": [ + 17, + 12, + 20, + 11, + 12 + ] + }, + { + "Número": "264", + "Nome": "Marcelo Sousa", + "Curso": "Ciência Política", + "Notas": [ + 18, + 19, + 19, + 20 + ] + } +] \ No newline at end of file diff --git a/TPC4/alunos4.csv b/TPC4/alunos4.csv new file mode 100644 index 0000000..e88adf6 --- /dev/null +++ b/TPC4/alunos4.csv @@ -0,0 +1,4 @@ +Número,Nome,Curso,Notas{3,5}::sum,,,,, +3162,Cândido Faísca,Teatro,12,13,14,, +7777,Cristiano Ronaldo,Desporto,17,12,20,11,12 +264,Marcelo Sousa,Ciência Política,18,19,19,20, diff --git a/TPC4/alunos4.json b/TPC4/alunos4.json new file mode 100644 index 0000000..98838e1 --- /dev/null +++ b/TPC4/alunos4.json @@ -0,0 +1,20 @@ +[ + { + "Número": "3162", + "Nome": "Cândido Faísca", + "Curso": "Teatro", + "Notas": 39 + }, + { + "Número": "7777", + "Nome": "Cristiano Ronaldo", + "Curso": "Desporto", + "Notas": 72 + }, + { + "Número": "264", + "Nome": "Marcelo Sousa", + "Curso": "Ciência Política", + "Notas": 76 + } +] \ No newline at end of file diff --git a/TPC4/alunos5.csv b/TPC4/alunos5.csv new file mode 100644 index 0000000..44b90f7 --- /dev/null +++ b/TPC4/alunos5.csv @@ -0,0 +1,4 @@ +Número,Nome,Curso,Notas{3,5}::media,,,,, +3162,Cândido Faísca,Teatro,12,13,14,, +7777,Cristiano Ronaldo,Desporto,17,12,20,11,12 +264,Marcelo Sousa,Ciência Política,18,19,19,20, diff --git a/TPC4/alunos5.json b/TPC4/alunos5.json new file mode 100644 index 0000000..4d82add --- /dev/null +++ b/TPC4/alunos5.json @@ -0,0 +1,20 @@ +[ + { + "Número": "3162", + "Nome": "Cândido Faísca", + "Curso": "Teatro", + "Notas": 13.0 + }, + { + "Número": "7777", + "Nome": "Cristiano Ronaldo", + "Curso": "Desporto", + "Notas": 14.4 + }, + { + "Número": "264", + "Nome": "Marcelo Sousa", + "Curso": "Ciência Política", + "Notas": 19.0 + } +] \ No newline at end of file diff --git a/TPC4/tpc4.py b/TPC4/tpc4.py new file mode 100644 index 0000000..ce786d2 --- /dev/null +++ b/TPC4/tpc4.py @@ -0,0 +1,65 @@ +import re +import json + +def parse_header(header): + regex = re.compile(r"(\w+)(?:{(?:(\d+),)?(\d+)}(?:::(\w+))?)?") + capture_list = regex.findall(header.strip()) + + header_order = [] + lists = {} + funcs = {} + + for id, min, max, func in capture_list: + header_order.append(id) + if max != "": + lists[id] = (int(min) if min else max, int(max)) + if func != "": + funcs[id] = func + return header_order, lists, funcs + + +def parse_data(lines, header_order, lists, funcs): + data_regex = "" + for id in header_order: + if id in lists: + min, max = lists[id] + if min == max: + num = '{' + str(max) + '}' + else: + num = '{' + str(min) + ',' + str(max) + '}' + data_regex += rf"(?P<{id}>([^,]+,?){num}),?" + else: + data_regex += rf"(?P<{id}>[^,]+),?" + data_regex = re.compile(data_regex) + + data = [] + for line in lines: + for match in data_regex.finditer(line.strip()): + data.append(match.groupdict()) + + for elem in data: + for id in header_order: + if id in lists: + elem[id] = [int(num) for num in re.findall(r"\d+",elem[id])] + if id in funcs: + if funcs[id] == "sum": + elem[id] = sum(elem[id]) + elif funcs[id] == "media": + elem[id] = sum(elem[id]) / len(elem[id]) + + return data + + +def main(): + for i in range(2,6): + test_file = f"alunos{i}" + f = open(f"{test_file}.csv", "r") + lines = f.readlines() + header_order, lists, funcs = parse_header(lines[0]) + + data = parse_data(lines[1:], header_order, lists, funcs) + json_file = open(f"{test_file}.json","w") + json.dump(data,json_file,indent=4,ensure_ascii=False) + +if __name__ == "__main__": + main()