Retrieving information from dictionary

2024/10/15 10:13:38

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'] = 1if self.dict['userID'] == 1:    # this is the line that is said to contain an errorthen do somethin ...

This is all I get as a Traceback:

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

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

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

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

Answer

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.

https://en.xdnf.cn/q/117842.html

Related Q&A

Python WMI Hyper-v GetSummaryInformation result

Im trying to retrieve information from all the available VMs on a Hyper-V Server. The problem is that when I ask for the summary information, i get a list of useless COMObjects.I cant find a way of get…

How to dynamically change variable name in form.vars.var_name

I have defined counter variable in controller.I can define tables and fields dynamically.tables = [db.define_table(example_table_%s % x,Field(example_field_%s % x, type=string, ...)...)for x in range(0…

Why will one loop modify a list of lists, but the other wont [duplicate]

This question already has answers here:Python list doesnt reflect variable change(6 answers)Closed 8 years ago.One of the answers in "python way" to parse and conditionally replace every elem…

Python CGI executable script downloads / shows script code

A script I wrote long ago (getWords.py) used to be executable at my localhost (http://local.example.com/getWords.py?query-string)My python script starts like this :#!/usr/bin/env python # chmod 755 ge…

My function returns None

I am new to Python and I was trying to solve this exercise, but keep getting None output. The question asked for a program in which the input is hours and rate and the output is the gross pay, includin…

Django undefined symbol: PyUnicode_AsUTF8

I am new to Python/Django. I have set up the environment needed to run Django project.When Im trying to migrate an existing project , it shows up this errordjango.core.exceptions.ImproperlyConfigured: …

using a variable keyword for an optional argument name with python argparse

I am using argparse for a python script I am writing. The purpose of the script is to process a large ascii file storing tabular data. The script just provides a convenient front-end for a class I have…

Error while setting up MongoDB with django using django mongodb engine on windows

Steps I followed :pip install git+htp://github.com/django-nonrel/[email protected]It did not work, so I downloaded the zip from the site "htp://github.com/django-nonrel/django" and pasted the…

Python login page with pop up windows

I want to access webpages and print the source codes with python, most of them require login at first place. I have similar problem before and I have solved it with the following code, because they are…

Calculate Scipy LOGNORM.CDF() and get the same answer as MS Excel LOGNORM.DIST

I am reproducing a chart in a paper using the LOGNORM.DIST in Microsoft Excel 2013 and would like to get the same chart in Python. I am getting the correct answer in excel, but not in python.In excel …