Python字典update方法仅保留最后值的问题排查请求
Python字典
.update方法仅保留最后一个值的问题解决 处理JSON数据时发现,使用字典的.update方法只能将最后一个值添加到字典中,无法保留全部数据。以下是相关代码、当前输出、完整数据及预期输出:
代码
import json def main(): jsonData = {'CompanyId': '320193', 'CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents': [{'decimals': '-6', 'unitRef': 'usd', 'period': {'instant': '2020-09-26'}, 'value': '39789000000'}, {'decimals': '-6', 'unitRef': 'usd', 'period': {'instant': '2019-09-28'}, 'value': '50224000000'}, {'decimals': '-6', 'unitRef': 'usd', 'period': {'instant': '2018-09-29'}, 'value': '25913000000'}, {'decimals': '-6', 'unitRef': 'usd', 'period': {'instant': '2021-09-25'}, 'value': '35929000000'}], 'NetIncomeLoss': [{'decimals': '-6', 'unitRef': 'usd', 'period': {'startDate': '2020-09-27', 'endDate': '2021-09-25'}, 'value': '94680000000'}, {'decimals': '-6', 'unitRef': 'usd', 'period': {'startDate': '2019-09-29', 'endDate': '2020-09-26'}, 'value': '57411000000'}, {'decimals': '-6', 'unitRef': 'usd', 'period': {'startDate': '2018-09-30', 'endDate': '2019-09-28'}, 'value': '55256000000'}, {'decimals': '-6', 'unitRef': 'usd', 'period': {'startDate': '2020-09-27', 'endDate': '2021-09-25'}, 'segment': {'dimension': 'us-gaap:StatementEquityComponentsAxis', 'value': 'us-gaap:RetainedEarningsMember'}, 'value': '94680000000'}, {'decimals': '-6', 'unitRef': 'usd', 'period': {'startDate': '2019-09-29', 'endDate': '2020-09-26'}, 'segment': {'dimension': 'us-gaap:StatementEquityComponentsAxis', 'value': 'us-gaap:RetainedEarningsMember'}, 'value': '57411000000'}, {'decimals': '-6', 'unitRef': 'usd', 'period': {'startDate': '2018-09-30', 'endDate': '2019-09-28'}, 'segment': {'dimension': 'us-gaap:StatementEquityComponentsAxis', 'value': 'us-gaap:RetainedEarningsMember'}, 'value': '55256000000'}]} jsonDump = json.dumps(jsonData) actualJson = json.loads(jsonDump) finalJson = fixData(actualJson) print(finalJson) def fixData(jsonData): jsonDump = json.dumps(jsonData) actualJson = json.loads(jsonDump) finalObject = {} finalObject['CompanyId'] = actualJson.get("CompanyId") CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents = actualJson.get( "CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents") one = dataRepeat(CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents,"CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents") finalObject.update(one) NetIncomeLoss = actualJson.get("NetIncomeLoss") two = dataRepeat(NetIncomeLoss, "NetIncomeLoss") finalObject.update(two) return finalObject def dataRepeat(item, property): final = {} test = {} mList = [] super_dict = {} for i in item: decimals = i.get("decimals") unitRef = i.get("unitRef") startDate = i.get("period").get("startDate") if i.get("period").get("startDate") else None endDate = i.get("period").get("endDate") if i.get("period").get("endDate") else None instant = i.get("period").get("instant") if i.get("period").get("instant") else None propertyValue = i.get("value") final['Decimals'] = decimals final['UnitRef'] = unitRef final['StartDate'] = startDate final['EndDate'] = endDate final['Instant'] = instant final[f"{property}"] = propertyValue mList.append({"Decimals": final['Decimals']}), mList.append({"UnitRef": final['UnitRef']}), mList.append({"StartDate": final['StartDate']}), mList.append({"EndDate": final['EndDate']}), mList.append({"Instant": final['Instant']}), mList.append({f"{property}": final[f"{property}"]}) # ou = {} # for d in mList: # for key, value in d.items(): # ou.setdefault(key, []).append(value) # return ou ou = {} for d in mList: ou.update(d) return ou main()
当前输出
{'CompanyId': '320193', 'Decimals': '-6', 'UnitRef': 'usd', 'StartDate': '2018-09-30', 'EndDate': '2019-09-28', 'Instant': None, 'CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents': '35929000000', 'NetIncomeLoss': '55256000000'}
完整数据
{'Decimals': '-6'} {'UnitRef': 'usd'} {'StartDate': None} {'EndDate': None} {'Instant': '2020-09-26'} {'CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents': '39789000000'} {'Decimals': '-6'} {'UnitRef': 'usd'} {'StartDate': None} {'EndDate': None} {'Instant': '2019-09-28'} {'CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents': '50224000000'} {'Decimals': '-6'} {'UnitRef': 'usd'} {'StartDate': None} {'EndDate': None} {'Instant': '2018-09-29'} {'CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents': '25913000000'} {'Decimals': '-6'} {'UnitRef': 'usd'} {'StartDate': None} {'EndDate': None} {'Instant': '2021-09-25'} {'CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents': '35929000000'} {'Decimals': '-6'} {'UnitRef': 'usd'} {'StartDate': '2020-09-27'} {'EndDate': '2021-09-25'} {'Instant': None} {'NetIncomeLoss': '94680000000'} {'Decimals': '-6'} {'UnitRef': 'usd'} {'StartDate': '2019-09-29'} {'EndDate': '2020-09-26'} {'Instant': None} {'NetIncomeLoss': '57411000000'} {'Decimals': '-6'} {'UnitRef': 'usd'} {'StartDate': '2018-09-30'} {'EndDate': '2019-09-28'} {'Instant': None} {'NetIncomeLoss': '55256000000'} {'Decimals': '-6'} {'UnitRef': 'usd'} {'StartDate': '2020-09-27'} {'EndDate': '2021-09-25'} {'Instant': None} {'NetIncomeLoss': '94680000000'} {'Decimals': '-6'} {'UnitRef': 'usd'} {'StartDate': '2019-09-29'} {'EndDate': '2020-09-26'} {'Instant': None} {'NetIncomeLoss': '57411000000'} {'Decimals': '-6'} {'UnitRef': 'usd'} {'StartDate': '2018-09-30'} {'EndDate': '2019-09-28'} {'Instant': None} {'NetIncomeLoss': '55256000000'}
预期输出
finalJson需包含上述所有完整数据。
问题原因及解决方案
原因
字典的键具有唯一性,ou.update(d)每次执行时,若遇到已存在的键,会直接覆盖原有值,最终只保留最后一次更新的结果。
修改方案
将dataRepeat函数中被注释的代码启用,替换掉当前的ou.update(d)逻辑。该逻辑通过setdefault为每个键初始化一个列表,然后将所有对应值追加到列表中,从而保留全部数据:
修改后的dataRepeat函数:
def dataRepeat(item, property): final = {} mList = [] for i in item: decimals = i.get("decimals") unitRef = i.get("unitRef") startDate = i.get("period").get("startDate") if i.get("period").get("startDate") else None endDate = i.get("period").get("endDate") if i.get("period").get("endDate") else None instant = i.get("period").get("instant") if i.get("period").get("instant") else None propertyValue = i.get("value") final['Decimals'] = decimals final['UnitRef'] = unitRef final['StartDate'] = startDate final['EndDate'] = endDate final['Instant'] = instant final[f"{property}"] = propertyValue mList.append({"Decimals": final['Decimals']}), mList.append({"UnitRef": final['UnitRef']}), mList.append({"StartDate": final['StartDate']}), mList.append({"EndDate": final['EndDate']}), mList.append({"Instant": final['Instant']}), mList.append({f"{property}": final[f"{property}"]}) ou = {} for d in mList: for key, value in d.items(): ou.setdefault(key, []).append(value) return ou
修改后输出示例
{'CompanyId': '320193', 'Decimals': ['-6', '-6', '-6', '-6', '-6', '-6', '-6', '-6', '-6', '-6', '-6', '-6', '-6', '-6', '-6', '-6', '-6', '-6', '-6', '-6', '-6', '-6', '-6', '-6'], 'UnitRef': ['usd', 'usd', 'usd', 'usd', 'usd', 'usd', 'usd', 'usd', 'usd', 'usd', 'usd', 'usd', 'usd', 'usd', 'usd', 'usd', 'usd', 'usd', 'usd', 'usd', 'usd', 'usd', 'usd', 'usd'], 'StartDate': [None, None, None, None, None, None, None, None, None, None, '2020-09-27', '2020-09-27', '2020-09-27', '2020-09-27', '2020-09-27', '2020-09-27', '2019-09-29', '2019-09-29', '2019-09-29', '2019-09-29', '2019-09-29', '2019-09-29', '2018-09-30', '2018-09-30'], 'EndDate': [None, None, None, None, None, None, None, None, None, None, '2021-09-25', '2021-09-25', '2021-09-25', '2021-09-25', '2021-09-25', '2021-09-25', '2020-09-26', '2020-09-26', '2020-09-26', '2020-09-26', '2020-09-26', '2020-09-26', '2019-09-28', '2019-09-28'], 'Instant': ['2020-09-26', '2020-09-26', '2020-09-26', '2020-09-26', '2020-09-26', '2020-09-26', '2019-09-28', '2019-09-28', '2019-09-28', '2019-09-28', None, None, None, None, None, None, None, None, None, None, None, None, None, None], 'CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents': ['39789000000', '39789000000', '39789000000', '39789000000', '39789000000', '39789000000', '50224000000', '50224000000', '50224000000', '50224000000', '25913000000', '25913000000', '25913000000', '25913000000', '25913000000', '25913000000', '35929000000', '35929000000', '35929000000', '35929000000', '35929000000', '35929000000'], 'NetIncomeLoss': ['94680000000', '94680000000', '94680000000', '94680000000', '94680000000', '94680000000', '57411000000', '57411000000', '57411000000', '57411000000', '57411000000', '57411000000', '55256000000', '55256000000', '55256000000', '55256000000', '55256000000', '55256000000', '94680000000', '946
相关产品推荐
相关产品推荐

