Extract values from specific parts of a . txt PYTHON

Asked

Viewed 45 times

0

I have a Test Base that I need to extract some values in a standard way, but several times I receive more than one value between the beginning and the last line that starts with 0089 (CONVENIO) in the quadrant.

With my code I can only make him write in txt the title I defined and he keeps repeating the first encounter, but I need him to go through the text and bring me the new information from the other quadrants.

I need the loop because it can have several covenants, for this reason I can’t "anchor" the lines directly into a size forecast.

import re
import os

inicio = ('  YM-INFRA-CASH MANAGMENT                                               DEST.: 001-0001-CENTRAL                                      ')


lista = []
contador = 3
banco = ' 0089'


convenio = ''

with open(caminho + '/Downloads/TESTE.txt', 'r') as arquivo:

    for line in arquivo:
        if line.strip() == inicio.strip():

            localizar = arquivo.readlines()

            inicio = localizar[contador]
            van = inicio[13:17]
            nomevan = inicio[20:50].strip()

            inicio = localizar[contador + 1]
            ag = inicio[13:17]
            nomeag = inicio[20:50].strip()

            inicio = localizar[contador + 2]
            cliente = inicio[13:50].strip()

            contadorum = 9

            while localizar[contadorum][1:5] == '0033':
                convenio = localizar[contadorum][1:22].strip()
                narqrem = localizar[contadorum][22:34].strip()
                bytesrem = localizar[contadorum][34:51].strip()
                narqret = localizar[contadorum][51:63].strip()
                bytesret = localizar[contadorum][63:81].strip()
                totalbytes = localizar[contadorum][81:99].strip()
                percrateio = localizar[contadorum][99:112].strip()

                print(van, nomevan)
                print(ag, nomeag)
                print(cliente)

                print(convenio, narqrem, bytesrem, narqret, bytesret, totalbytes, percrateio)
                lista.append(convenio + narqrem + bytesrem + narqret + bytesret + totalbytes + percrateio +'\n')


                with open(caminho + '/Downloads/testefim.txt', 'w') as consolidado:
                    consolidado.write('CONVENIO' + ';' + 'N ARQ REMES'  + ';' + 'BYTES REMES' + ';' + 'N ARQ.RET.' + ';' + 'BYTES RET.' + ';' + 'TOTAL BYTES' + ';' + '% RATEIO' + '\n')
                    for linha in lista:
                        consolidado.write(convenio + ';' + narqrem + ';' + bytesrem + ';' + narqret + ';' + bytesret + ';' + totalbytes + ';' + percrateio + '\n')

                    consolidado.close()
            else:
                pass


    arquivo.close()
  • I wanted the person who bothers to click -1 to at least explain why they did it. If I am asking is because I have spent a lot of time researching and exhausted my little knowledge on this subject. I have done this code 3 times, through other searches and this so far was the best I could, but still does not deliver me the necessary result.

  • 1

    Why not do this: (a) read file, (b) iterate each line, (c) test whether the line starts with "0089". if linha.startswith("0089"). Then you process the line...

  • That was the light I needed!!! /\

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.