简体   繁体   English

Python:如何修改嵌套字典中的值并返回整个字典

[英]Python: How to modify values in nested dictionary and have the whole dict returned

Imagine that I have got a file named 'test_dict.txt' containing the following dictionary-like text: 想象一下,我有一个名为“ test_dict.txt”的文件,其中包含以下类似于字典的文本:

{
    "subdic1" : {
        "a_string" : "something1",
        "a_integer" : 16,
        "a_list" : [
            "str_1",
            "str_2"
        ]
    },
    "subdic2" : {
        "a_string" : "something2",
        "a_integer" : 32,
        "a_list" : [
            "str_3",
            "str_4"
        ]
    }
}

As you can see that there are nested dictionaries. 如您所见,有嵌套的字典。 What I want to do is to convert all deepest values ("something1", 16, ["str_1", "str_2"], etc) to unicode type objects, for some comparison purposes. 我想要做的就是将所有最深的值(“ something1”,16 [[str_1”,“ str_2”]等)转换为unicode类型的对象,以进行比较。 Here is my attempt: 这是我的尝试:

import json
from copy import deepcopy

def to_unicode(d):
    dc = deepcopy(d)
    for k,v in dc.iteritems():
        if isinstance(v, dict):
            to_unicode(v)
        else:
            dc[k] = unicode(v)
    return dc

dict_fname = 'test_dict.txt'
with open(dict_fname) as dict_fd:
    dic = json.load(dict_fd)
print dic
print to_unicode(dic)

I used recursion in my function 'to_unicode' in order to traverse to the deepest values. 为了遍历最深的值,我在函数“ to_unicode”中使用了递归。 The first 'print' gives the result of 'json.load' operation like the following: 第一个“打印”给出“ json.load”操作的结果,如下所示:

{u'subdic1': {u'a_list': [u'str_1', u'str_2'], u'a_integer': 16, u'a_string': u'something1'}, u'subdic2': {u'a_list': [u'str_3', u'str_4'], u'a_integer': 32, u'a_string': u'something2'}}

So what I should really convert to unicode type is the two integers 16 and 32. But I still want the function to convert each value inside each level of dictionary for the purpose of simplicity. 因此,我真正应该转换为unicode类型的是两个整数16和32。但是为了简化起见,我仍然希望该函数在字典的每个级别内转换每个值。 The two numbers are supposed to be converted to u'16' and u'32', so the dictionary object returned by the function should be printed like this: 应该将这两个数字转换为u'16'和u'32',因此该函数返回的字典对象应该像这样打印:

{u'subdic1': {u'a_list': [u'str_1', u'str_2'], u'a_integer': u'16', u'a_string': u'something1'}, u'subdic2': {u'a_list': [u'str_3', u'str_4'], u'a_integer': u'32', u'a_string': u'something2'}}

But in fact my second 'print' gives the exactly same result as the first one. 但实际上,我的第二张“印刷品”给出的结果与第一张印刷品完全相同。 I guess the issue occurs either in deepcopy or in the way of return of the function, or even both. 我猜问题可能是在Deepcopy中或在函数返回的方式中发生的,或者甚至同时发生。 I really want the whole dictionary to be returned back after the conversion, rather than yielding one item a time. 我真的希望整个字典在转换后返回,而不是一次产生一项。 Could somebody help correcting my code please? 有人可以帮忙更正我的代码吗?

As @jonrsharpe mentioned you just need to assign things back to the master or original copy. 正如@jonrsharpe提到的,您只需要将内容分配回原版或原始副本即可。 Here's some spaghetti that iterates on the code you've provided : 这是一些意大利细面条,可根据您提供的代码进行迭代:

def to_unicode(d, target_dict={}):
    for k,v in d.iteritems(): 
        if isinstance(v, dict): 
            target_dict = to_unicode(v, target_dict)
        else: 
            target_dict[k] = unicode(v)
    return target_dict

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM