Advertisement
fvasconcelos

Código em Python que analisa um arquivo texto

Jun 4th, 2016
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.60 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import sys
  3.  
  4. # coding=utf8
  5.  
  6. #   PafKit
  7. #   Jent Software
  8. #   Copyright 2016
  9.  
  10. errors = 0
  11. lines = 0
  12.  
  13. print("\x1b[2J\x1b[1;1H")  # Limpa a tela
  14. print("Pafkit analisa o arquivo do anexo IV do seu PAF-ECF\nVersão 0.1\n<<< Renomeie o arquivo a ser analisado para \"anexoIV.txt\" >>>\nJent Software - www.jent.com.br\n\n")
  15.  
  16. # Opening the file
  17. fileObject = open('/home/jent/projects/PafKit/anexoIV.txt', 'r')
  18. # INSERIR AQUI OS TRATAMENTOS NO CASO DO ARQUIVO NÃO ABRIR
  19. fileRead = fileObject.read()
  20. fileRead = fileRead.upper()  # This uppers the string
  21.  
  22. # Separating lines
  23. fileReadSplitted = (fileRead.split('\n'))  # This separate all lines of the file
  24.  
  25. if not fileObject.read(0): #This line provides a mechanism that denies empty files
  26.   print("Erro fatal: arquivo vazio. Análise abortada. (F02)\n")
  27.   sys.exit()
  28.  
  29. #Counting the number of lines
  30. for currLine in fileObject:
  31.   lines += 1
  32.  
  33. # Registro do tipo U1
  34.  
  35. def U1func():
  36.   global errors
  37.  
  38.   if not fileReadSplitted[0].startswith('U1'):
  39.     print("Erro fatal: Registro do tipo U1 não está presente no arquivo. Não é possível prosseguir com a análise. (F01)\n")
  40.     sys.exit()
  41.   elif not fileReadSplitted[0][2:15].isdecimal():
  42.     print('Linha ' + str(fileReadSplitted[0]))
  43.     print('Erro: O CNPJ ' + str(fileReadSplitted[0][2:15]) + ' não é composto exclusivamente por caracteres decimais. (U01)\n')
  44.     errors += 1
  45.  
  46. def countErrors():
  47.   if errors > 0:
  48.     print('Erros encontrados: ' + str(errors))
  49.   elif errors == 0:
  50.     print('Não foram encontrados erros na análise deste arquivo!')
  51.  
  52. U1func()
  53. countErrors()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement