<p>您在这里犯了两个错误。<br/>
一个是您正在使用类变量(请阅读此处:<a href="https://docs.python.org/3/tutorial/classes.html" rel="nofollow noreferrer">https://docs.python.org/3/tutorial/classes.html</a>)<br/>
第二个是Gokai说的<a href="https://stackoverflow.com/a/63034395/3683639">here</a></p>
<p>这应该起作用:</p>
<pre><code>import xml.etree.ElementTree as ET
class Invoice:
"""
Common tasks in relation to EHF invoices.
"""
def __init__(self, invoice):
"""Initialize invoice attributes."""
self.invoice = invoice
self.namespace = {
'cac': 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-3',
'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-3',
'ext': 'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-3',
'ccts': 'urn:un:unece:uncefact:documentation:1',
'xsi': 'http://www.w2.org/2001/XMLSchema-instance'
}
self.attachments = {}
def encoded_attachments(self):
"""
Return the embedded attachments from the EHF invoice in encoded form
as a dictionary.
Keys = filenames
Value = base64 encoded files
"""
for document in self.invoice.findall('cac:AdditonalDocumentReference', self.namespace):
# Find filename
filename = document.find('cbc:ID', self.namespace).text
# Find the embedded file
for child in document.findall('cac:Attachment', self.namespace):
# Add filename and attachment to dictionary
self.attachments[filename] = child.find('cbc:EmbeddedDocumentBinaryObject', self.namespace).text
return self.attachments
</code></pre>