子索引超出范围,python元素树 [英] Child index out of range, python element tree

查看:63
本文介绍了子索引超出范围,python元素树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试运行此代码时收到从未收到的错误。

I am receiving an error I have never received before when trying to run this code.

文件 BasicEmail.py,第96行,位于init_ui根目录[0] [1] .text
IndexError:子索引超出范围
中止陷阱:6

我的代码很简单,

class EmailBlast(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        user_file = 'user_info.xml'
        tree = ET.parse(user_file)
        root = tree.getroot()
        root[0][1].text
        self.emailLabel = QtWidgets.QLabel("Email:")
        self.emailListLabel = QtWidgets.QLabel("")
        self.sendButton = QtWidgets.QPushButton("Save")
        self.settingsButton = QtWidgets.QPushButton("Settings")

        h_box = QtWidgets.QHBoxLayout()
        h_box.addStretch()

        v_box = QtWidgets.QVBoxLayout()
        v_box.addWidget(self.emailLabel)
        v_box.addWidget(self.emailListLabel)
        v_box.addWidget(self.sendButton)
        v_box.addWidget(self.settingsButton)        
        v_box.addLayout(h_box)

        self.setLayout(v_box)
        self.setWindowTitle("Email Blast")

        self.settingsButton.clicked.connect(lambda: self.settings(self.settingsButton, "Saved"))        
        self.show()

    def settings(self, settingsButton, string):
        self.ui = ConfigWindow()
        self.hide()        
        print("Settings")

我能够获取标签和属性,没有值。 XML中的数据很好,应该有一个数组或列表可供我提取。

I am able to get the tags and attributes, no values. The data in the XML is fine and there should be an array, or list, there for me to pull from.

包含的完整xml文件:

Included full xml file:

<data>
<email>ewokhugz@gmail.com</email>
<password>testpass</password>
<smtp>gmail</smtp>
<port>587</port>
</data>`


推荐答案

您的数据xml模式将子级直接显示在根
中,因此无需访问嵌套子级:

Your data xml pattern presents the children directly inside the root, so no need to access a nested child:

root = tree.getroot()
root[0].text # returns the email
root[1].text # returns the password
root[2].text # returns the smtp 
root[3].text # returns the port

您也可以使用名称查询来允许更改您的模式:

You can also use the name query to allow some change in your pattern :

root.find('email').text 

这篇关于子索引超出范围,python元素树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆