简体   繁体   中英

Python: how to remove timezone from dictionary values?

I've a dictionary defined as follows. Its values are both str and datetime type. How to loop through it and remove timezones?

{'Account': 'xxx', 'Group': 'yyy', 'CreationDate': pywintypes.datetime(2020, 12, 2, 15, 58, 28, 790000, tzinfo=TimeZoneInfo('GMT Standard Time', True)), 'EditDate': pywintypes.datetime(2020, 12, 16, 10, 46, 7, 519000, tzinfo=TimeZoneInfo('GMT Standard Time', True))}

I tried this way but I get error 'dict_values' object is not subscriptable on tmp = contacts_dict.values()[j] :

contacts_dict_len = len(contacts_dict)

for j in range(0,contacts_dict_len):
    tmp = contacts_dict.values()[j]
    if isinstance(tmp, datetime.datetime):
        contacts_dict.values()[j] = tmp.replace(tzinfo=None)

PS: Removing timezones is needed in order to avoid following exception:

AttributeError: 'NoneType' object has no attribute 'total_seconds'
Exception ignored in: 'pandas._libs.tslibs.conversion._localize_tso'

Python dictionaries are not subscriptable. Remove the timezones by directly addressing the key values that you wish to change instead of looping through them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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