【发布时间】:2021-06-04 23:18:03
【问题描述】:
我正在尝试修改data 字典,以便仅dictionary 中的名称RSI, MOM 保留在data 字典中。我如何创建一个函数来过滤和查找字典 data 和 dictionary 中的等效名称,然后删除其他所有内容?
代码:
dictionary= {'Account1': {'RSI': OrderedDict([('Exchange', 'Bybit'), ('AccountName', 'Account1'), ('StrategyName', 'RSI'), ('Script', 'MomentumStrats'), ('StratStatus', 'ACTIVE')]),'MOM': OrderedDict([('Exchange', 'Bybit'), ('AccountName', 'Account1'), ('StrategyName', 'MOM'), ('Script', 'MomentumStrats'), ('StratStatus', 'ACTIVE')])}, 'Account2': {}}
def reading():
with open('data.json') as f:
data = json.load(f)
return data
reading()
预期输出:
{
"RSI": [
{
"TradingPair": "BTCUSD",
"fetchSubscriptions": [0],
}
],
"MOM":[
{
"TradingPair": "BCHUSDT",
"fetchSubscriptions": [0],
}
]
}
JSON 文件:
{
"RSI": [
{
"TradingPair": "BTCUSD",
"fetchSubscriptions": [0],
}
],
"MOM":[
{
"TradingPair": "BCHUSDT",
"fetchSubscriptions": [0],
}
],
"MOM_RSI":[
{
"TradingPair": "BTCUSDT",
"fetchSubscriptions": [0],
}
]
}
【问题讨论】:
标签: json python-3.x function dictionary format