Comparing strings

Asked

Viewed 67 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(text,"r", encoding="utf8")
for linha in arquivo:
    lista = linha
    
#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 lista is None:
        if lista in EAN:
            print(EAN)
            print("EAN encontrado!")
            #print("#------------------------------#")
        else:
            print(lista)
            print("EAN nao encontrado!")
            #print("#------------------------------#")
    else:
        print(lista)
        print("EAN nao encontrado!")
        #print("#------------------------------#")
#obtem o campo EAN e verifica se foi encontrado ou nao. 

But the problem is that no matter if the data are equal, the code still responds that it does not find and I do not know why.

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    
7898622140258   

If anyone can help me I’d appreciate it please

  • What is the content of the first file (the one pointed out by text)? The idea is to see which words of this file appear in XML?

  • updated with txt, yes that’s the idea

  • That one None in the file means that if the EAN is empty it must also be found?

  • That one none can come from xml so I made a if to check if it is none if she is not wanted

  • 1

    If EAN for None means the tag is empty so you wouldn’t even need to have it in the first file

1 answer

2


First of all, it’s wrong around here:

for linha in arquivo:
    lista = linha

You read all lines of the file, but with each iteration you override the value of lista. At the end, she’ll only have the last line read.

If the idea is to have a list of the words to be searched, then create a list with the contents of the file:

with open(text,"r", encoding="utf8") as arquivo:
    lista = [ linha.strip() for linha in arquivo ]

I used with to open the file, so it will already be closed automatically. And I used strip to delete spaces at the beginning and end, in addition to line breaks that are also returned when reading from a file.

Then, when reading the XML, just see if the EAN is on the list (you were doing the opposite, seeing if the list was on EAN):

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

    if EAN in lista: # verifica se o EAN está na lista
        print(EAN)
        print("EAN encontrado!")
    else:
        print("EAN nao encontrado!")

Detail: the first line of the txt file will result in the string "None" (and not in value None Python), so I think that line can be removed from there, because it doesn’t make sense. If the value of EAN for None, means that the tag is empty, and then it won’t even be on the list (i.e., the if EAN in lista is enough to know if he’s on the list or not).

  • Truth I was having the reverse logic, that detail does not need to be checked even

Browser other questions tagged

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