How to extract specific data from a python text file and store in a variable

Asked

Viewed 94 times

-3

Good morning, I have a text file with more than 2mil tax coupons , but I wanted to extract one by one for the value, for now my code is like this:

import re

txt = open("arqEspelho.txt", "r")

x = re.findall("^cotia", txt)

z = re.findall("ibpt$", txt)

for linhas in txt:
   if x and z and "172,20":

    print("tem")

else:

    print("No match")

but I can’t return the coupon

i need to tell the function that word "COTIA" is the beginning of the coupon and the word "IBPT" is the end of a coupon and if a certain value is among those words, he returns me this coupon.

thank you in advance.

  • 1

    Place a part of the file arqEspelho.txt.

1 answer

0

I don’t know how it’s yours arqEspelho.txt then created one to present a possible solution

arqEspelho.txt

cotia
1 pao 10,00
2 carnes 20,00
ibpt
cotia
1 batata 15,00
2 cerveja 172,20
3 calabresa 25,50
ibpt
cotia
1 feijao 22,00
2 paio 12,45
3 laranja 32,23
ibpt

Solution

import re

txt = open("arqEspelho.txt", "r").read()

# usando finditer ele retorna a posição da palavra
x = re.finditer(r"cotia", txt)
z = re.finditer(r"ibpt", txt)

# estou considerando que o arquivo possui a mesma quantidades de "cotia" e "ibpt"
# e que elas abrem e fecham cada espelho respectivamente
espelhos = list(zip(x, z))

# Testando o valor específico para cada espelho
for espelho in espelhos:
    txt_espelho = txt[espelho[0].span()[0]: espelho[1].span()[1]+1]
    if "172,20" in txt_espelho:
       print("Tem")
    else:
       print("Nao tem")

The exit will be

Nao tem
Tem
Nao tem

Update from the comment

To print the special mirror

for espelho in espelhos:
    txt_espelho = txt[espelho[0].span()[0]: espelho[1].span()[1]+1]
    if "172,20" in txt_espelho:
       print(txt_espelho)

The exit will be

cotia
1 batata 15,00
2 cerveja 172,20
3 calabresa 25,50
ibpt
  • This , but it would have to return the coupon on the way out instead of "Has" ?

  • Use print(txt_mirror)

  • Good night friend , I am again would have to return the low line type the line below the IBPT , because the bottom line generates random numbers and can not find with the finditer , since I thank!

  • I return to the comment made by Bernardo Lopes, put a part of the file arqEspelho.txt. Perhaps it is best to accept this answer as a solution to your initial question and start another question.

Browser other questions tagged

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