Skip to content

Instantly share code, notes, and snippets.

@CrociDB
Created March 16, 2019 20:14
Show Gist options
  • Save CrociDB/03bb41f3d6738a065ed4c2e2bcfd7bd9 to your computer and use it in GitHub Desktop.
Save CrociDB/03bb41f3d6738a065ed4c2e2bcfd7bd9 to your computer and use it in GitHub Desktop.

Revisions

  1. CrociDB created this gist Mar 16, 2019.
    84 changes: 84 additions & 0 deletions trabalho.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    import re
    import sys
    import os

    def main():
    print("Digita ai um arquivo pra gente abrir ou digita 'sair' pra sair (duh): ")
    for linha in sys.stdin:
    nome = linha.rstrip()
    if nome.strip().lower() == "sair": return

    if os.path.isfile('./' + nome):
    checa_ficha(nome)

    print("\n > Digita ai um arquivo pra gente abrir ou digita 'sair' pra sair (duh): ")


    def checa_ficha(nome_arquivo):
    arquivo = open(nome_arquivo, 'r', encoding="utf8")

    # Primeira linha do arquivo: Nome
    linha_nome = arquivo.readline()
    nome = re.search("^Nome: .*$", linha_nome)
    if nome == None:
    print("CABECALHO INVALIDO")
    return

    # Segunda linha do arquivo: CPF
    linha_cpf = arquivo.readline()
    cpf = re.search("^CPF: [0-9]{3}\.[0-9]{3}\.[0-9]{3}-[0-9]{2}$", linha_cpf)
    if cpf == None:
    print("CABECALHO INVALIDO")
    return

    # Terceira linha do arquivo: Matricula
    linha_matricula = arquivo.readline()
    matricula = re.search("^Matricula: [0-9]{10}$", linha_matricula)
    if matricula == None:
    print("CABECALHO INVALIDO")
    exit()

    print("CABECALHO VALIDO")

    # Esse readline é pra pular a linha de separação
    arquivo.readline()

    regex_gigante_hehe = re.compile('^(20[0-9]{2}\/[1-2]-[1-2]) ([A-Z]{6}[0-9]{3}) ["“]([A-z0-9 ]+)["”] ([A-Z]{3}[0-9]{2}_[A-Z][0-9]{2}) ([0-9][,.][0-9]{2}) ([0-9]{1,2}[,.][0-9]{2}) (APROVADO|REPROVADO)$')

    total_credito = 0.0
    total_nota_cred = 0.0

    disciplinas_invalidas = []

    i = 1
    for linha in arquivo:
    r = regex_gigante_hehe.search(linha)
    ano_semestre = r.group(1)
    disciplina_cod = r.group(2)
    disciplina_nome = r.group(3)
    turma = r.group(4)
    credito = float(r.group(5).replace(',', '.'))
    nota = float(r.group(6).replace(',', '.'))
    status = r.group(7)

    total_credito += credito
    total_nota_cred += credito * nota

    if (nota < 6.0 and status.upper() != 'REPROVADO') or (nota >= 6.0 and status.upper() != "APROVADO"):
    disciplinas_invalidas.append(i)

    i += 1
    #print(ano_semestre, disciplina_cod, disciplina_nome, turma, credito, nota, status)

    if len(disciplinas_invalidas) > 0:
    print("LINHAS INVALIDAS")
    for l in disciplinas_invalidas:
    print('LINHA %02d' % (l))

    cre = total_nota_cred / total_credito
    print('CRE: %03.2f' % (cre))

    arquivo.close()

    if __name__ == '__main__':
    main()