遍历字典并修改值

2024-06-28 19:01:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过递归迭代dict的键将dict中的特定日期时间值更改为字符串。在

from copy import deepcopy
def datetoStr2(datadict):
if 'dict' in str(type(datadict)):
    newdict = dict()
    newdict.update(datadict)
    for key in datadict:
        if isinstance(datadict[key], dict):
            newdict[key] = datetoStr(datadict[key])
        if isinstance(datadict[key], list):
            mylist = datadict[key]
            for element in mylist:
                element = datetoStr(element)
        if isinstance(datadict[key], datetime):
        #newdict[key] = datadict[key].strftime('%Y-%m-%d %H:%M:%S.%f%z')
            newdict[key] = str(datadict[key])
    return newdict
else:
    return datadict

但是,当我跑的时候

^{pr2}$

我仍然保留着日期时间对象。我做错什么了?在

编辑:更新功能代码。在

编写这个函数是因为这个dict是通过Flask的jsonify传递的,该jsonify当前删除了时区信息,并用GMT替换它(这将在下一次更新中修复)。在

我已经更新了我的函数,记住了一些反馈,但是我仍然无法产生结果。在

我正在添加一些示例数据和我的输出。在

结果:

在代码中插入的一个快速打印告诉我datetime仍然是一个datetime,并且TZinfo被jsonify剥离。在

样本数据:

{'data': {'all': {'count': 2,
                  'data': [{'arrivalby': 'Wed, 20 Jun 2018 09:39:00 GMT',
                            'closesat': 'Wed, 20 Jun 2018 06:05:40 GMT',
                            'doctype': 'general',
                            'geofence': {'coordinates': [[[78.43786473803105,
                                                           17.447430456650043],
                                                          [78.45670406196896,
                                                           17.447430456650043],
                                                          [78.45670406196896,
                                                           17.46540214334996],
                                                          [78.43786473803105,
                                                           17.46540214334996],
                                                          [78.43786473803105,
                                                           17.447430456650043]]],
                                         'type': 'Polygon'},
                            'housecallid': '591ce42e9b53f96ea393934f61fab25d',
                            'lastupdated': 'Fri, 22 Jun 2018 04:32:14 GMT',
                            'requester': {'addr1': 'xxx xxx xx',
                                          'addr2': 'xx xx xx',
                                          'addr3': 'xx xx',
                                          'addrstate': 'xx',
                                          'city': 'xx',
                                          'dob': 'xx/xx/xxxx',
                                          'email': 'xx@xx',
                                          'fname': 'xxx',
                                          'lname': 'xx',
                                          'location': {'coordinates': [78.4472844,
                                                                       17.4564163],
                                                       'type': 'Point'},
                                          'mobile': xxxx,
                                          'sex': 'xxxx'},
                            'requesttime': 'Wed, 20 Jun 2018 00:05:40 GMT',
                            'responder': {'addr1': 'xxx xxxx',
                                          'addr2': 'xx xx xxxx',
                                          'addr3': 'xxx xx',
                                          'addrstate': 'xxxx',
                                          'city': 'xxxx',
                                          'email': 'xx@xxxx',
                                          'fname': 'xxxx',
                                          'lname': 'xxxx',
                                          'location': {'coordinates': [78.4471904,
                                                                       17.456458],
                                                       'type': 'Point'},
                                          'mobile': xxxx,
                                          'sex': 'xxxx'},
                            'responsetime': 'Wed, 20 Jun 2018 00:09:37 GMT',
                            'status': 'missed'},
                           {'arrivalby': 'Sat, 23 Jun 2018 10:13:00 GMT',
                            'closesat': 'Fri, 22 Jun 2018 10:42:11 GMT',
                            'doctype': 'general',
                            'geofence': {'coordinates': [[[78.43784273760974,
                                                           17.44743855664762],
                                                          [78.45668206239024,
                                                           17.44743855664762],
                                                          [78.45668206239024,
                                                           17.465410243352377],
                                                          [78.43784273760974,
                                                           17.465410243352377],
                                                          [78.43784273760974,
                                                           17.44743855664762]]],
                                         'type': 'Polygon'},
                            'housecallid': '998987171790f172b97a7981a8902af1',
                            'lastupdated': 'Fri, 22 Jun 2018 18:36:50 GMT',
                            'requester': {'addr1': 'xx xxx, ',
                                          'addr2': 'xx xx xxxx',
                                          'addr3': 'xxx xxx',
                                          'addrstate': 'xx',
                                          'city': 'xxxx',
                                          'dob': 'xxxx-xx-xx',
                                          'email': 'xxxx@xxxx',
                                          'fname': 'xx',
                                          'lname': 'xx',
                                          'location': {'coordinates': [78.4472624,
                                                                       17.4564244],
                                                       'type': 'Point'},
                                          'mobile': xxxx,
                                          'sex': 'xxxx'},
                            'requesttime': 'Fri, 22 Jun 2018 04:42:11 GMT',
                            'responder': {'addr1': 'xxxx xxxx',
                                          'addr2': 'xx xx xx',
                                          'addr3': 'xxxx xxxx',
                                          'addrstate': 'xxxx',
                                          'city': 'xxxx',
                                          'email': 'xxx@xxxx',
                                          'fname': 'xxxxx',
                                          'lname': 'xxxx',
                                          'location': {'coordinates': [78.4471904,
                                                                       17.456458],
                                                       'type': 'Point'},
                                          'mobile': xxxx,
                                          'sex': 'xx'},
                            'responsetime': 'Fri, 22 Jun 2018 04:43:05 GMT',
                            'status': 'missed'}]},
          'cancelled': {'count': 0, 'data': []},
          'closed': {'count': 0, 'data': []},
          'confirmed': {'count': 0, 'data': []},
          'missed': {'count': 2,
                     'data': [{'arrivalby': 'Wed, 20 Jun 2018 09:39:00 GMT',
                               'closesat': 'Wed, 20 Jun 2018 06:05:40 GMT',
                               'doctype': 'general',
                               'geofence': {'coordinates': [[[78.43786473803105,
                                                              17.447430456650043],
                                                             [78.45670406196896,
                                                              17.447430456650043],
                                                             [78.45670406196896,
                                                              17.46540214334996],
                                                             [78.43786473803105,
                                                              17.46540214334996],
                                                             [78.43786473803105,
                                                              17.447430456650043]]],
                                            'type': 'Polygon'},
                               'housecallid': '591ce42e9b53f96ea393934f61fab25d',
                               'lastupdated': 'Fri, 22 Jun 2018 04:32:14 GMT',
                               'requester': {'addr1': 'xx xx xxx',
                                             'addr2': 'xx xx xxxx',
                                             'addr3': 'xx xx',
                                             'addrstate': 'xxxxxx',
                                             'city': 'xxxxx',
                                             'dob': 'xx/xx/xxxx',
                                             'email': 'xx@xxx',
                                             'fname': 'xx',
                                             'lname': 'xxx',
                                             'location': {'coordinates': [78.4472844,
                                                                          17.4564163],
                                                          'type': 'Point'},
                                             'mobile': xx,
                                             'sex': 'xx'},
                               'requesttime': 'Wed, 20 Jun 2018 00:05:40 GMT',
                               'responder': {'addr1': 'xx xx',
                                             'addr2': 'xx xx xxxx',
                                             'addr3': 'xx xx',
                                             'addrstate': 'xx',
                                             'city': 'xx',
                                             'email': 'xx@xx',
                                             'fname': 'xx',
                                             'lname': 'xx',
                                             'location': {'coordinates': [78.4471904,
                                                                          17.456458],
                                                          'type': 'Point'},
                                             'mobile': xxxx,
                                             'sex': 'xx'},
                               'responsetime': 'Wed, 20 Jun 2018 00:09:37 GMT',
                               'status': 'missed'},
                              {'arrivalby': 'Sat, 23 Jun 2018 10:13:00 GMT',
                               'closesat': 'Fri, 22 Jun 2018 10:42:11 GMT',
                               'doctype': 'general',
                               'geofence': {'coordinates': [[[78.43784273760974,
                                                              17.44743855664762],
                                                             [78.45668206239024,
                                                              17.44743855664762],
                                                             [78.45668206239024,
                                                              17.465410243352377],
                                                             [78.43784273760974,
                                                              17.465410243352377],
                                                             [78.43784273760974,
                                                              17.44743855664762]]],
                                            'type': 'Polygon'},
                               'housecallid': '998987171790f172b97a7981a8902af1',
                               'lastupdated': 'Fri, 22 Jun 2018 18:36:50 GMT',
                               'requester': {'addr1': 'xxx xxx, ',
                                             'addr2': 'xx xx xxxx',
                                             'addr3': 'xx xx',
                                             'addrstate': 'xxx',
                                             'city': 'xxx',
                                             'dob': 'xxxx-xx-xx',
                                             'email': 'xxxx@xxxx',
                                             'fname': 'xx',
                                             'lname': 'xx',
                                             'location': {'coordinates': [78.4472624,
                                                                          17.4564244],
                                                          'type': 'Point'},
                                             'mobile': xxxxx,
                                             'sex': 'xx'},
                               'requesttime': 'Fri, 22 Jun 2018 04:42:11 GMT',
                               'responder': {'addr1': 'xx xx',
                                             'addr2': 'xx xx xxxx',
                                             'addr3': 'xx xx',
                                             'addrstate': 'xx',
                                             'city': 'xxxx',
                                             'email': 'xxxx@xxxx',
                                             'fname': 'xx',
                                             'lname': 'xx',
                                             'location': {'coordinates': [78.4471904,
                                                                          17.456458],
                                                          'type': 'Point'},
                                             'mobile': xx,
                                             'sex': 'xxxx'},
                               'responsetime': 'Fri, 22 Jun 2018 04:43:05 GMT',
                               'status': 'missed'}]}},  'success': True}

更新-2018年6月24日

谢谢你,帕特里克,你的回答。我接受了它,认为它是正确的。在

然而,在我的编辑中,我谈到了列表是一个元素的想法,因此我添加了以下代码(对于那些想绕过jsonify bug的人)。在

def datetoStr(datadict):
    from copy import deepcopy
    # deepcopy before data manipulation
    if 'dict' in str(type(datadict)):
        newdict = deepcopy(datadict)

        for key,value in datadict.items():
            # recurse into nested dicts
            if isinstance(value, dict):
                newdict[key] = datetoStr(datadict[key])
            # convert to string
            elif isinstance(value, datetime):
                newdict[key] = str(datadict[key])
            # if list, iterate through elements and replace
            elif isinstance(datadict[key], list):
                newlist = datadict[key].copy()
                counter = 0
                for element in datadict[key]:
                    newlist[counter] = datetoStr(datadict[key][counter])
                    counter += 1
                newdict[key] = newlist
        return newdict
    else:
        return datadict

Tags: keytypedictjunxxxxxgmtcoordinates
1条回答
网友
1楼 · 发布于 2024-06-28 19:01:54

如果使用^{},可以稍微简化代码,它直接为迭代提供键和值:

from copy import deepcopy
import datetime 

def datetoStr(datadict):
    # deepcopy before data manipulation
    newdict = deepcopy(datadict)

    for key,value in datadict.items():
        # recurse into nested dicts
        if isinstance(value, dict):
            newdict[key] = datetoStr(datadict[key])
        # convert to string
        elif isinstance(value, datetime.datetime): 
            newdict[key] = str(datadict[key])

    return newdict


before = { "a date": datetime.datetime.now(),
           "a dict": { "another": datetime.datetime.now() + datetime.timedelta(hours=2)}}

print(before)

# get the modified copy of before
transformedDict = datetoStr(before) 
print(transformedDict)

输出:

^{pr2}$

相关问题 更多 >