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
about the
None
I had this why can I come an empty xml value– user198451
@Valterferreira But an empty XML value is valid or invalid?
– hkotsubo
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>
 lista = [ linha.split()[0] for linha in arquivo ]
IndexError: list index out of range
– user198451
@Valterferreira This error happens if you have a blank line in the file, because there
split
returns an empty list. Anyway, ifNone
or empty is invalid, the code I made already serves, because these values will not be in the list– hkotsubo
@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)
– hkotsubo
@Valterferreira Ops, was incomplete, now yes I updated the complete code
– hkotsubo
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?– user198451
@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 putNone
, he’s not getting into theif EAN in lista
and will be considered invalid (as seems to be what you want). That’s not what you need?– hkotsubo
now yes I understood I had not seen the change excuse
– user198451