简体   繁体   English

Python:如何删除字典中的符号?

[英]Python: How to remove symbol in dict?

I get a xml response which is in bytes and parsed by xmltodict as shown below. 我得到一个以字节为单位的xml响应,并由xmltodict解析,如下所示。 Since I need to further use the response, I want to eliminate all the '@' in the key. 由于我需要进一步使用响应,因此我想消除键中的所有“ @”。 I have tried two way to remove the sign but still not working. 我尝试了两种方法来删除标志,但仍然无法正常工作。 Are there any method to remove sign in dict? 有什么方法可以删除登录字典?

XML response (resp.text): {'HOLDING_QUERY_RESPONSE': OrderedDict([('@LOGIN_ID', '011'), ('@CLIENT_ID', '011'), ('@CHANNEL', 'MB'), ('@SESSION_KEY', '111'), ('RESULT', OrderedDict([('@STATUS', 'OK'), ('BUYING_POWER', '98’),( 'USD_BUYING_POWER', '12'), ('AVAILABLE_MKT_VAL', '110'), ('CASH_HOLDING', OrderedDict([('LINEITEM', [OrderedDict([('@CURRENCY_CODE', 'USD'), ('@AVAILABLE_BAL', '-233'), ('@CASH_BALANCE', '0.00'), ('@CASH_ON_HOLD', '0.00'), ('@UNSETTLED_CASH', '0.00'), ('@ACCRUED_INTEREST', '0.00'), ('@BUY_SOLD_CONSIDERATION', '-233'), ('@EX_RATE', '1'), ('@AVAILABLE_VAL', '6'), ('@AMT', '0.00'), ('@DUE_AMT', '0.00')]),…….)])])]))]))])}

First tried method. 首先尝试的方法。 The error is dict object has no attribute 'iteritems()'. 错误是字典对象没有属性'iteritems()'。 If I remove the '.iteritem()', it will show Value Error: too many value to unpack. 如果删除“ .iteritem()”,它将显示“值错误”:太多值无法解包。

info = {key.replace('@',''): item for key, item in xmltodict.parse(resp.text).iteritems()}
print(info)

Second tried method is changed it to string first and then translate it back to dict. 第二种尝试方法将其首先更改为字符串,然后将其转换回dict。 But, still it seems the dict has not translate to string by json.dumps. 但是,似乎该字典还没有通过json.dumps转换为字符串。

info = json.dumps(resp.text)
info.replace('@','')
to_dict = json.loads(info)
print(to_dict)

Try this: 尝试这个:

info = {key.replace('@',''): item for key, item in xmltodict.parse(resp.text).items()}
print(info)

As mentioned in the comments, xmltodict.parse returns a dictionary, and in your case, you might be working with Python 3, so use the .items() attribute to access the dictionary. 如注释中所述, xmltodict.parse返回一个字典,在您的情况下,您可能正在使用Python 3,因此请使用.items()属性访问字典。

As an example: 举个例子:

d = ('@CASH', '0.00'), ('@INTEREST', '0.00'), ('@BUY', '-233'), ('@RATE', '1.000000'), ('@AVAILABLE', '6,228'), ('@AMT', '0.00')
info = {key.replace('@',''): item for key, item in d}

print(info)
>>
{'CASH': '0.00', 'INTEREST': '0.00', 'BUY': '-233', 'RATE': '1.000000', 'AVAILABLE': '6,228', 'AMT': '0.00'}

Updated question soution 更新的问题状态

Similarly, if it is a nested dictionary, you have to access the key you want to replace @ in its values, from your updated example, you should be able to access the nested dictionary with xmltodict.parse(resp.text)['HOLDING_QUERY_RESPONSE'] . 同样,如果它是嵌套字典,则必须访问其值中要替换@的键,在更新的示例中,您应该能够使用xmltodict.parse(resp.text)['HOLDING_QUERY_RESPONSE']

from collections import OrderedDict

#based on your updated data structure
d = OrderedDict([('@CASH', '0.00'), ('@INTEREST', '0.00'), ('@BUY', '-233'), ('@RATE', '1.000000'), ('@AVAILABLE', '6,228'), ('@AMT', '0.00')])
xml_response = {}
xml_response['HOLDING_QUERY_RESPONSE'] = d

xml_response
{'HOLDING_QUERY_RESPONSE': OrderedDict([('@CASH', '0.00'),
              ('@INTEREST', '0.00'),
              ('@BUY', '-233'),
              ('@RATE', '1.000000'),
              ('@AVAILABLE', '6,228'),
              ('@AMT', '0.00')])}

info = {key.replace('@',''):value for key, value in xml_response['HOLDING_QUERY_RESPONSE'].items()}
print(info)

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

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