从字典中检索信息 [英] Retrieving information from dictionary

查看:81
本文介绍了从字典中检索信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难读取我的字典变量. Python不断抛出以下错误:

I'm having hard time trying to read my dictionary variable. Python keeps throwing the following error:

TypeError: string indices must be integers

这是一个示例,应该使您了解我的问题是什么

This is a sample that should give you an idea of what my problem is:

self.dict = {}
self.dict['User'] = 'User_name'
self.dict['Dir'] = 'user_home_dir'
self.dict['userID'] = 1

if self.dict['userID'] == 1:    # this is the line that is said to contain an error
    then do somethin ...

这就是我得到的回溯信息:

This is all I get as a Traceback:

user_id = self.dict['userID']
TypeError: string indices must be integers

不,我很肯定self.dict不是字符串.

No I'm positive that self.dict is not a string.

它是在一个名为createUser的函数中创建的,并返回到主线程.

It's being created in a function called createUser and it's being returned to the main thread.

def createUser(self):
    self.dict = {}

    ... user inputs information ...
    ... and then information is inserted into self.dict ...

    self.dict['User'] = self.lineEdit_User.text()
    self.dict['Dir'] = self.lineEdit_path.text()
    self.dict['userID'] = int(self.lineEdit_ID.text())

    return self.dict

此后,仅引用self.dict以便从中读取数据.

After that self.dict is only referenced for reading data from it.

推荐答案

我已经找到问题所在,并且在我的信号定义中.字典'self.dict'通过pyqtSignal返回,其定义如下:

I've found the problem, and it was in my signal definition. Dictionary 'self.dict' is being returned via pyqtSignal which was defined like this:

addUser = pyqtSignal(object)

仔细检查我的代码后,我发现应该将其更改为:

After thorough examination of my code I've figured out that I should change it to:

addUser = pyqtSignal(dict)

一旦我做到了,那就好了.再次感谢大家的贡献.

and once I did that it was all right. Once again, thanks everyone for their contribution.

这篇关于从字典中检索信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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