postgresql 如何将值插入嵌套的Python字典

clj7thdc  于 2023-04-05  发布在  PostgreSQL
关注(0)|答案(1)|浏览(101)

我试图将值插入到嵌套的Python字典中。我得到的响应来自postgresql数据库,其中表有四列:

coe,  coe_type, count, coe_status
Author 1, Open,       10,    Published
Author 2, Closed,     20,    Not-Published

etc....

目前我的回复是这样的

"data": {
    "Author 1": {
      "Open": {},
      "Closed": {},
      "All": {
        "Published": 1,
        "Non-Published": 1
      }
    },

我遇到的问题是,我想插入每个特定类型的所有计数。例如,Open应该有自己的Published和Non-Published计数。Closed也是如此。
所以响应应该是这样的

"data": {
    "beryl": {
      "Open": {
        "Published": 1,
        "Non-Published": 1
     },
      "Closed": {
        "Published": 1,
        "Non-Published": 1
      },
      "All": {
        "Published": 1,
        "Non-Published": 1
      }
    },

下面是当前代码的编写方式:

category_headers = ["Open", "Published"]
book_main_headers = ["Owner", "Published", "Non-Published"]
book_type_headers=["Open", "Closed", "All"]

        response_result = {}
        last_author = None
        response = execute_query(sql_query, transaction_id, False)

            current_list = ast.literal_eval(row)
            current_author, book, book_count, book_published_non_published = (current_list[0], current_list[1], current_list[2], current_list[3])

            if last_author is None or last_author != current_author:
                interim_dictionary[str(current_author)] = {} 
                last_author = current_author
            for book_type in book_type_headers:
                interim_dictionary[str(current_author)][str(book_type)] = {} 
            for coe_category in category_headers:
                interim_dictionary[str(current_author)][str(book_type)][str(book_category)] = {} 
                if book_category not in interim_dictionary[str(current_author)]:
                    interim_dictionary[str(current_author)][str(book_type)][book_category] = 0
                if book == 'Open':
                    if book_type_headers == 'Published':
                       interim_dictionary[str(current_author)][str(book_type)][book_category] = book_count
                    else:
                        interim_dictionary[str(current_author)][str(book_type)][book_category] = book_count
                if book == 'Closed':
                    if book_type_headers == 'Published':
                       interim_dictionary[str(current_author)][str(book_type)][book_category] = book_count
                    else:
                        interim_dictionary[str(current_author)][str(book_type)][book_category] = book_count
                else:
                    if book_type_headers == 'Published':
                       interim_dictionary[str(current_author)][str(book_type)][book_category] = book_count
                    else:
                        interim_dictionary[str(current_author)][str(book_type)][book_category] = book_count
zpgglvta

zpgglvta1#

你的代码中有一些问题,首先,你的第二个for循环在第一个循环之外,这意味着book_type的值在代码的其余部分总是All,而且第19行的if似乎没有检查正确的条件:不应该是book_category not in interim_dictionary[str(current_author)][str(book_type)]而不是book_category not in interim_dictionary[str(current_author)]吗?你也在用字符串比较列表:if book_type_headers == 'Published':。最后,从第25行开始的所有行都做同样的事情,所以if是无用的。
下面是我如何编写这段代码:

category_headers = ["Open", "Published"]
book_main_headers = ["Owner", "Published", "Non-Published"]
book_type_headers=["Open", "Closed", "All"]

response_result = {}
last_author = None
response = execute_query(sql_query, transaction_id, False)

current_list = ast.literal_eval(row)
current_author, book_type, book_count, book_published_non_published = (current_list[0], current_list[1], current_list[2], current_list[3])

if last_author is None or last_author != current_author:
    # Create empty dictionnary for the author
    interim_dictionary[str(current_author)] = { book_type: { category: 0 for category in category_headers } for book_type in book_type_headers }
    last_author = current_author

    # Add books to 'All'
    interim_dictionary[str(current_author)]['All'][book_published_non_published] += book_count

    # Add books to the subdictionnary with proper type
    interim_dictionary[str(current_author)][book_type][book_published_non_published] += book_count

相关问题