Python中文
首页
教程
问答
标签
搜索
登录
注册
Python类返回空字典
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>新手需要一些帮助使代码面向对象</p> <p>我试图用不同的方法编写一个类来处理XML文件。其中一种方法的目标是返回一个字典,其中嵌入附件的文件名和编码的数据字符串分别作为键和值</p> <p>我已经设法让它在课外发挥作用:</p> <pre class="lang-py prettyprint-override"><code>import xml.etree.ElementTree as ET tree = ET.parse('invoice.xml') root = tree.getroot() namespace = { 'cac': 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2', 'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2', 'ext': 'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2', 'ccts': 'urn:un:unece:uncefact:documentation:2', 'xsi': 'http://www.w3.org/2001/XMLSchema-instance' } attachments = {} for document in root.findall('cac:AdditionalDocumentReference', namespace): filename = document.find('cbc:ID', namespace).text print(filename) # Find the embedded file for child in document.findall('cac:Attachment', namespace): attachment = child.find('cbc:EmbeddedDocumentBinaryObject', namespace).text attachments[filename] = attachment </code></pre> <p>但我无法将其转换为类方法,因为类方法返回一个空字典。我正在编写的代码:</p> <pre class="lang-py prettyprint-override"><code>import xml.etree.ElementTree as ET class Invoice: """ Common tasks in relation to EHF invoices. """ namespace = { 'cac': 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2', 'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2', 'ext': 'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2', 'ccts': 'urn:un:unece:uncefact:documentation:2', 'xsi': 'http://www.w3.org/2001/XMLSchema-instance' } attachments = {} def __init__(self, invoice): """Initialize invoice attributes.""" self.invoice = invoice # Dictionary for namespace used in EHF invoices self.namespace = self.namespace 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', namespace): attachment = child.find('cbc:EmbeddedDocumentBinaryObject', self.namespace).text # Add filename and attachment to dictionary self.attachments[filename] = attachment return(self.attachments) tree = ET.parse('invoice.xml') root = tree.getroot() ehf = Invoice(root) attach_dict = ehf.encoded_attachments() print(attach_dict) </code></pre> <p>我认为我在课堂上遗漏了一些重要的东西。感谢您的帮助</p> <p>编辑:</p> <p>xml文件的一部分。用伪文本字符串替换编码数据</p> <pre class="lang-xml prettyprint-override"><code><?xml version="1.0" encoding="UTF-8"?> <Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:ccts="urn:un:unece:uncefact:documentation:2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <cbc:CustomizationID>urn:cen.eu:en16931:2017#compliant#urn:fdc:peppol.eu:2017:poacc:billing:3.0</cbc:CustomizationID> <cbc:ProfileID>urn:fdc:peppol.eu:2017:poacc:billing:01:1.0</cbc:ProfileID> <cbc:ID>1060649</cbc:ID> <cbc:IssueDate>2020-01-23</cbc:IssueDate> <cbc:DueDate>2020-02-07</cbc:DueDate> <cbc:InvoiceTypeCode>380</cbc:InvoiceTypeCode> <cbc:TaxPointDate>2020-01-23</cbc:TaxPointDate> <cbc:DocumentCurrencyCode>NOK</cbc:DocumentCurrencyCode> <cbc:BuyerReference>N/A</cbc:BuyerReference> <cac:AdditionalDocumentReference> <cbc:ID>invoice_attachment_filename.pdf</cbc:ID> <cbc:DocumentTypeCode>130</cbc:DocumentTypeCode> <cbc:DocumentDescription>CommercialInvoice</cbc:DocumentDescription> <cac:Attachment> <cbc:EmbeddedDocumentBinaryObject mimeCode="application/pdf" filename="1060649.pdf">BASE64ENCODEDTEXT</cbc:EmbeddedDocumentBinaryObject> </cac:Attachment> </cac:AdditionalDocumentReference> </Invoice> </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p><code>self</code>的用法不一致</p> <pre><code>for child in document.findall('cac:Attachment', **namespace**): attachment = child.find('cbc:EmbeddedDocumentBinaryObject', **self.namespace**).text </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
VirtualEnvRapper错误:路径python2(来自python=python2)不存在
6 回答
virtualenvs上的pyinstaller,没有名为导入错误的模块
10 回答
virtualenvs是否可以退回到用户包而不是系统包?
8 回答
virtualenvwrapper CentOS7
7 回答
virtualenvwrapper IOError:[Errno 13]权限被拒绝
9 回答
virtualenvwrapper mkproject和shell在windows中的启动问题?
5 回答
virtualenvwrapper mkvirtualenv不工作但没有错误
3 回答
Virtualenvwrapper python bash
7 回答
virtualenvwrapper:“workon”何时更改到项目目录?
4 回答
virtualenvwrapper:mkvirtualenv可以工作,但是rmvirtualenv返回bash:没有这样的文件或目录
9 回答
virtualenvwrapper:virtualenv信息存储在哪里?
6 回答
virtualenvwrapper:命令“python设置.pyegg_info“失败,错误代码为1
8 回答
virtualenvwrapper:如何将mkvirtualenv的默认Python版本/路径更改为ins
5 回答
Virtualenvwrapper:模块“pkg_resources”没有属性“iter_entry_points”
8 回答
Virtualenvwrapper:没有名为virtualenvwrapp的模块
3 回答
Virtualenvwrapper.bash_profi的正确设置
3 回答
Virtualenvwrapper.hook:权限被拒绝
2 回答
virtualenvwrapper.sh:fork:资源暂时不可用Python/Djang
5 回答
Virtualenvwrapper.shlssitepackages命令不工作
5 回答
Virtualenvwrapper.sh函数在bash sh中不可用
4 回答