Looking for string inside another Python string

Asked

Viewed 63 times

0

Hello I have a program to check if there is a string inside a file like this:

import xml.etree.ElementTree as ET
#Bibliotecas
#------------------------------------------------------------------------------------------

#text = input("Coloque o diretorio do fisico: ").strip('"')
arquivo = open(r"C:\Users\Expedição\Videos\arquivo_text\contagem_11.08.20-09.03.36(2) - Copia.txt","r", encoding="utf8")
for linha in arquivo:
    lista = linha.strip('[]').strip('\n')
    #print(lista[:13])
    
#localiza arquivo,lista todos os itens
#------------------------------------------------------------------------------------------

XML = input("Coloque o diretorio do XML: ").strip('"')
tree = ET.parse(XML)
root = tree.getroot()
print('\n')

ns = {'nfe': 'http://www.portalfiscal.inf.br/nfe'}
for det in root.findall('.//nfe:det', ns):
    nItem = det.attrib['nItem']
    quantidade = det.find('.//nfe:qCom', ns).text.strip('000').strip('.')
    EAN = det.find('.//nfe:cEAN', ns).text

#importa XML, obtem os campos EAN,quatidade    
#------------------------------------------------------------------------------------------

    if EAN is not None:
        if EAN in lista[:13]:
            print(EAN)
            print("EAN encontrado!")
            #print("#------------------------------#")
        else:
            print(EAN)
            print("EAN INVALIDO!")
            #print("#------------------------------#")
    else:
        print(EAN)
        print("EAN INVALIDO!")
        #print("#------------------------------#")

#obtem o campo EAN e verifica se foi encontrado ou nao. 

it was working the more I needed to change the logic a little and so I started using if EAN in lista[:13]: in place of if EAN in lista: because the return and only of numerical values is possible to confuse him. But I can’t make it work anymore I gave a print(lista[:13])is what I think the return of txt is ok. THE XML:

<nfeProc xmlns="http://www.portalfiscal.inf.br/nfe" versao="4.00">
<NFe xmlns="http://www.portalfiscal.inf.br/nfe">
<infNFe Id="NFe35200811625533000185550010000043291000219014" versao="4.00">
<ide>
...
</ide>
<emit>
...
</emit>
<dest>
...
</dest>
<det nItem="1">
<prod>
<cProd>ET-CAM 01</cProd>
<cEAN/>
<xProd>CAMERA ESTAC MISTA (FURADA + SUP. BORBOLETA)</xProd>
<NCM>85258029</NCM>
<CEST>2106300</CEST>
<CFOP>5405</CFOP>
<uCom>UNI</uCom>
<qCom>20.0000</qCom>
<vUnCom>22.0000</vUnCom>
<vProd>440.00</vProd>
<cEANTrib/>
<uTrib>UNI</uTrib>
<qTrib>20.0000</qTrib>
<vUnTrib>22.0000</vUnTrib>
<indTot>1</indTot>
</prod>
<imposto>
...
</imposto>
</det>
<det nItem="2">
<prod>
<cProd>ET-CAM 02</cProd>
<cEAN>7898622140258</cEAN>
<xProd>CAMERA ESTAC DIANTEIRA</xProd>
<NCM>85258029</NCM>
<CEST>2106300</CEST>
<CFOP>5405</CFOP>
<uCom>UNI</uCom>
<qCom>10.0000</qCom>
<vUnCom>26.9000</vUnCom>
<vProd>269.00</vProd>
<cEANTrib>7898622140258</cEANTrib>
<uTrib>UNI</uTrib>
<qTrib>10.0000</qTrib>
<vUnTrib>26.9000</vUnTrib>
<indTot>1</indTot>
</prod>

The TXT file:

None    20
7898622140258   10
7898622140630   10

1 answer

1


I think I’ve mentioned another question from you, that for is wrong:

for linha in arquivo:
    lista = linha.strip('[]').strip('\n')

In this way, the variable lista is overwritten with each iteration of for, and at the end it will only have a single string, corresponding to the last line of the file.

And I’ve also suggested that you use with, for so the file is automatically closed at the end. Anyway, if the idea is to have a file line on each line, save everything in a list.

And if that’s the contents of the file:

None    20
7898622140258   10
7898622140630   10

And you only want the first part, you can use split to separate into 2 parts, and only take the first. Using [:13] will end up catching the whole first line, because it has less than 13 characters.

Another detail is that in the first line the None will be converted to the string "None", and not the value None python. But if the value of EAN for None, he will no longer be on the list, and in your code you seem to treat None as invalid, so I think the first line can be removed from the file:

with open('contagem.txt', "r", encoding="utf8") as arquivo:
    lista = [ linha.split()[0] for linha in arquivo ]

import xml.etree.ElementTree as ET
tree = ET.parse('nfe.xml')
root = tree.getroot()

ns = {'nfe': 'http://www.portalfiscal.inf.br/nfe'}
for det in root.findall('.//nfe:det', ns):
    EAN = det.find('.//nfe:cEAN', ns).text

    if EAN in lista:
        print(f"EAN encontrado: {EAN}")
    else:
        print(f"EAN INVALIDO: {EAN}")

If EAN for None, it will not be in the list, and then it will already be invalid. It does not need a if specific to this case.

And using split, I already separate each line into two parts (using the spaces as separator), and then I take only the first, which seems to be the part that matters (remembering that the first line of txt can be eliminated).


There is only one detail: if the file has any empty line, the split return an empty list and error when accessing the zero index. Then you can include a treatment for this:

def tratar_linha(linha):
    dados = linha.split()
    if len(dados) > 0:
        return dados[0]
    return None

lista = []
with open('contagem.txt', "r", encoding="utf8") as arquivo:
    for linha in arquivo:
        dado = tratar_linha(linha)
        if dado is not None:
            lista.append(dado)

So you only add the data if you have something on the line.

  • about the None I had this why can I come an empty xml value

  • @Valterferreira But an empty XML value is valid or invalid?

  • an empty value in XML is invalid and I tbm tested what showed me by returning the following error: File "c:/Users/Expedição/Videos/python/Consulta_Interface.py", line 7, in <listcomp>&#xA; lista = [ linha.split()[0] for linha in arquivo ]&#xA;IndexError: list index out of range

  • @Valterferreira This error happens if you have a blank line in the file, because there split returns an empty list. Anyway, if None or empty is invalid, the code I made already serves, because these values will not be in the list

  • @Valterferreira I updated the answer with an alternative to treating the blank lines (but I think the ideal would be to remove them from the file)

  • @Valterferreira Ops, was incomplete, now yes I updated the complete code

  • I understood that this happens because the list can not store empty right, but comes an empty xml, it is not possible to make a split because of this error above, so how do I fix? convert the list into a string?

  • @Valterferreira The split is not in xml, it is in txt. I read txt and build the list with the values valid, so you don’t need to have empty neither None in this list. In xml, if the field is empty, its value will be None, and as on the list I did not put None, he’s not getting into the if EAN in lista and will be considered invalid (as seems to be what you want). That’s not what you need?

  • now yes I understood I had not seen the change excuse

Show 4 more comments

Browser other questions tagged

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