[英]Nested dictionary to single dictionary
So, i'm building a little tool to save errors and their solutions as a knowledge base. 因此,我正在构建一个小的工具来将错误及其解决方案保存为知识库。 It is stored in a SQL Database (i'm using pyodbc). 它存储在SQL数据库中(我正在使用pyodbc)。 The users don't have access to the database, just the GUI. 用户无权访问数据库,只有GUI。 The GUI has three buttons, one for add a new ErrorID, one for search for an ErrorID (if it exists in the database), and one for delete. GUI具有三个按钮,一个按钮用于添加新的ErrorID,一个按钮用于搜索ErrorID(如果数据库中存在),另一个按钮用于删除。 It has too a text panel where it should show the solution of the error searched. 它也有一个文本面板,该面板应显示搜索到的错误的解决方案。
So, need to extract the columns and rows of my DB and put them in a dictionary, then I need to run through that dict in search for the error searched and show it solution on the text panel. 因此,需要提取数据库的列和行并将它们放入字典中,然后我需要遍历该字典以搜索所搜索的错误,并在文本面板上显示解决方案。 My issue is that the dict that I get has this form: {{('Error', 1): ('Solution', one)}} and so on, so I cannot seem to run succesfully through it and show ONLY the word "one" on the text panel. 我的问题是,我得到的字典具有以下形式:{{('Error',1):('Solution',one)}},依此类推,所以我似乎无法成功地通过它来仅显示单词文本面板上的“一个”。 In other words, when I search "1", it should print "one" on the text panel. 换句话说,当我搜索“ 1”时,它应该在文本面板上打印“ 1”。
My question is: How can I transform this {{('Error', 1): ('Solution', one)}} INTO this {"1": "one"} ? 我的问题是:如何将这个{{('Error',1):('Solution',one)}}转换成这个{“ 1”:“ one”}?
Edit: Sorry, I forgot to add some parts of my code. 编辑:对不起,我忘了添加代码的某些部分。
This part is what appends every row in a dict: 这部分是将字典的每一行追加的内容:
readsql = sql_conn.sql()
readsql.sqlread()
columns = [column[0] for column in readsql.cursorv.description]
results = []
for row in readsql.cursorv.fetchall():
results.append(zip(columns, row))
results = dict(results)
I tried to do this, like storing part of the dict that I know it's gonna show on a string named, well, string. 我试图做到这一点,就像存储我知道它将在名为string的字符串上显示的dict的一部分一样。 And then compare it to 'k' in the for loop, but it doesn't work. 然后将其与for循环中的“ k”进行比较,但这不起作用。
string = "('Error', " + str(error_number) + ")"
for k in results.keys():
if k == string:
post = readsql.cursorv.execute('SELECT * FROM master.dbo.Errors WHERE Error = (?)', (error_number))
text_area.WriteText(post)
break
Here is sql class: 这是sql类:
class sql():
def __init__(self):
self.conn = pyodbc.connect('Driver={SQL Server};'
'Server=.\SQLEXPRESS;'
'Database=master;'
'Trusted_Connection=yes;')
# cursor variable
self.cursorv = self.conn.cursor()
def sqlread(self):
self.cursorv.execute('SELECT * FROM master.dbo.Errors')
Your problem comes from the following code unnecessarily zipping the column headers into the resulting dict: 您的问题来自以下代码,这些代码不必要将列标题压缩到结果字典中:
for row in readsql.cursorv.fetchall():
results.append(zip(columns, row))
results = dict(results)
You can instead construct the desired dict directly from the sequence of tuples returned by the fetchall
method: 您可以直接从fetchall
方法返回的元组序列构造所需的dict:
results = dict(readsql.cursorv.fetchall())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.