我试图读取一个xml修改一些标签并在输出中打印,但是python在输出文件中添加“NS0”和“NS1”。我做了一些测试并删除了“NS0”,但我无法删除“NS1”。输入XML如下:
<?xml version="1.0" encoding="UTF-8" ?>
<nfeProc versao="3.10" xmlns="http://www.portalfiscal.inf.br/nfe">
<NFe xmlns="http://www.portalfiscal.inf.br/nfe">
<infNFe versao="3.10" Id="NFe53171205246123000120650010000000011152346962">
<ide>
<cUF>53</cUF>
</ide>
<emit>
<CNPJ>05246123000120</CNPJ>
<xNome>TESTE</xNome>
<xFant>TESTE</xFant>
</emit>
<det nItem="1">
<prod>
<cProd>1</cProd>
<cEAN/>
<xProd>USING</xProd>
<NCM>12333</NCM>
<comb>
<cProdANP>23333111</cProdANP>
<UFCons>DF</UFCons>
<encerrante>
<nBico>20</nBico>
<nBomba>5</nBomba>
<nTanque>6</nTanque>
<vEncIni>1503366.039</vEncIni>
<vEncFin>1503368.540</vEncFin>
</encerrante>
</comb>
</prod>
</det>
</infNFe>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<Reference URI="#NFe53171205246123000120650010000000011152346962">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>LZ5vw12GA+Iqp7h94N3UyuWmzKQ=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>asdsd</SignatureValue>
<KeyInfo>
<X509Data>
<X509Certificate>asd321321</X509Certificate>
</X509Data>
</KeyInfo>
</Signature>
</NFe>
</nfeProc>
代码是:
import xml.etree.ElementTree as ET
import re
hashBico = {};
hashTanque = {};
with open('bin\\de_para_bico.txt', 'rU') as fIn:
for line in fIn.readlines():
line.strip();
splited = line.split(';');
CNPJ = re.sub("\D", "", splited[2]);
bicoAntigo = splited[3];
bicoNovo = splited[5].strip();
hashBico[CNPJ, bicoAntigo] = bicoNovo;
fIn.close();
with open('bin\\de_para_tanque.txt', 'rU') as fIn:
for line in fIn.readlines():
line = line.strip();
splited = line.split(';');
CNPJ = re.sub("\D", "", splited[2]);
tanqueAntigo = splited[3];
tanqueNovo = splited[5];
hashTanque[CNPJ, tanqueAntigo] = tanqueNovo;
fIn.close();
with open('bin\\filelist.txt', 'rU') as fIn:
for line in fIn.readlines():
arq = 'entrada\\'+line.strip();
with open(arq, 'rU') as fInArq:
# for lineArq in fInArq.readlines():
lineArq = fInArq.read();
lineArq=lineArq.strip();
# ET.register_namespace('', "http://www.w3.org/2000/09/xmldsig#");
ET.register_namespace('', "http://www.portalfiscal.inf.br/nfe");
tree = ET.parse(arq);
root = tree.getroot();
tag = '{http://www.portalfiscal.inf.br/nfe}'
seacher = '{}NFe/{}infNFe/{}det/{}prod/{}comb/{}encerrante'.format(tag, tag, tag, tag, tag, tag)
CNPJ = root.find('{}NFe/{}infNFe/{}emit/{}CNPJ'.format(tag, tag, tag, tag));
for item in root.findall(seacher):
nBico = '{}nBico'.format(tag);
nTanque = '{}nTanque'.format(tag);
item.find(nBico).text = hashBico[CNPJ.text, item.find(nBico).text];
item.find(nTanque).text = hashTanque[CNPJ.text, item.find(nTanque).text];
# print(CNPJ.text, item.find(nBico).text );
# print(CNPJ.text, item.find(nTanque).text);
output = arq.replace("entrada", "saida", 1);
tree.write(output)
fInArq.close();
fIn.close();
正如您可以在下面看到的那样“ET.register_namespace('',”http://www.portalfiscal.inf.br/nfe“);”删除“NS0:”但仍然出现“NS1:”我怎么能删除它?
<nfeProc xmlns="http://www.portalfiscal.inf.br/nfe" xmlns:ns1="http://www.w3.org/2000/09/xmldsig#" versao="3.10">
<NFe>
<infNFe Id="NFe53171205246123000120650010000000011152346962" versao="3.10">
<ide>
<cUF>53</cUF>
</ide>
<emit>
<CNPJ>05246123000120</CNPJ>
<xNome>TESTE</xNome>
<xFant>TESTE</xFant>
</emit>
<det nItem="1">
<prod>
<cProd>1</cProd>
<cEAN />
<xProd>USING</xProd>
<NCM>12333</NCM>
<comb>
<cProdANP>23333111</cProdANP>
<UFCons>DF</UFCons>
<encerrante>
<nBico>8</nBico>
<nBomba>5</nBomba>
<nTanque>2</nTanque>
<vEncIni>1503366.039</vEncIni>
<vEncFin>1503368.540</vEncFin>
</encerrante>
</comb>
</prod>
</det>
</infNFe>
<ns1:Signature>
<ns1:SignedInfo>
<ns1:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<ns1:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<ns1:Reference URI="#NFe53171205246123000120650010000000011152346962">
<ns1:Transforms>
<ns1:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
<ns1:Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
</ns1:Transforms>
<ns1:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<ns1:DigestValue>LZ5vw12GA+Iqp7h94N3UyuWmzKQ=</ns1:DigestValue>
</ns1:Reference>
</ns1:SignedInfo>
<ns1:SignatureValue>asdsd</ns1:SignatureValue>
<ns1:KeyInfo>
<ns1:X509Data>
<ns1:X509Certificate>asd321321</ns1:X509Certificate>
</ns1:X509Data>
</ns1:KeyInfo>
</ns1:Signature>
</NFe>
</nfeProc>
我尝试了许多不同的方式,包括:
ET.register_namespace('', "http://www.w3.org/2000/09/xmldsig#");
ET.register_namespace('', "http://www.portalfiscal.inf.br/nfe");
tree = ET.parse(arq);
root = tree.getroot();
但仍在使用它。