我是Python的新手,希望有人可以帮助我 我试过这个,但是我收到了一个错误:TypeError:类型' NoneType'是不可迭代的 我认为问题是某些价值是没有的,我不能删除没有的东西吗?
etheurticker= finexticker.get('ETH/EUR')
if 'askVolume' and 'bidVolume' and 'vwap' and 'open' and 'previousClose' and
'change' and 'percentage' and 'baseVolume' and 'quoteVolume' and 'info'and
'average' in etheurticker:
del etheurticker['askVolume']
del etheurticker['bidVolume']
del etheurticker['vwap']
del etheurticker['open']
del etheurticker['previousClose']
del etheurticker['change']
del etheurticker['percentage']
del etheurticker['baseVolume']
del etheurticker['quoteVolume']
del etheurticker['info']
del etheurticker['average']
这是没有清算的字典:
{'symbol': 'ETH/EUR', 'timestamp': 1529160540908.2085, 'datetime': '2018-06-
16T14:49:01.908Z', 'high': 440.45, 'low': 416.57356472, 'bid': 422.75,
'bidVolume': None, 'ask': 422.77, 'askVolume': None, 'vwap': None, 'open':None, 'close': 422.76, 'last': 422.76, 'previousClose': None, 'change': None, 'percentage': None, 'average': 422.76, 'baseVolume': 3318.1630349899992, 'quoteVolume': None, 'info': {'mid': '422.76', 'bid': '422.75', 'ask': '422.77', 'last_price': '422.76', 'low': '416.57356472', 'high': '440.45', 'volume': '3318.1630349899992', 'timestamp': '1529160540.908208614', 'pair': 'ETHEUR'}}
希望有人可以帮助我:)。
答案 0 :(得分:1)
你可以像这样删除它们:
etheurticker = {'symbol': 'ETH/EUR', 'timestamp': 1529160540908.2085,
'datetime': '2018-06-16T14:49:01.908Z',
'high': 440.45, 'low': 416.57356472, 'bid': 422.75,
'bidVolume': None, 'ask': 422.77, 'askVolume': None, 'vwap': None,
'open':None, 'close': 422.76, 'last': 422.76, 'previousClose': None,
'change': None, 'percentage': None, 'average': 422.76,
'baseVolume': 3318.1630349899992, 'quoteVolume': None,
'info': {'mid': '422.76', 'bid': '422.75', 'ask': '422.77',
'last_price': '422.76', 'low': '416.57356472', 'high': '440.45',
'volume':'3318.1630349899992', 'timestamp':'1529160540.908208614',
'pair': 'ETHEUR'}}
print(etheurticker)
print("\nafter:\n")
# all things that must be in the dict so you delete them:
data = ['askVolume' ,'bidVolume' ,'vwap' , 'open' ,'previousClose', 'change',
'percentage', 'baseVolume','quoteVolume','info', 'average']
# check if all are given
if all(k in etheurticker for k in data):
for n in data : # iterate names to delete them
del etheurticker[n]
print(etheurticker)
输出:
{'symbol': 'ETH/EUR', 'timestamp': 1529160540908.2085, 'datetime': '2018-06-16T14:49:01.908Z',
'high': 440.45, 'low': 416.57356472, 'bid': 422.75, 'bidVolume': None,
'ask': 422.77, 'askVolume': None, 'vwap': None, 'open': None, 'close': 422.76,
'last': 422.76, 'previousClose': None, 'change': None, 'percentage': None,
'average': 422.76, 'baseVolume': 3318.1630349899992, 'quoteVolume': None,
'info': {'mid': '422.76', 'bid': '422.75', 'ask': '422.77', 'last_price': '422.76', 'low': '416.57356472', 'high': '440.45', 'volume': '3318.1630349899992', 'timestamp': '1529160540.908208614', 'pair': 'ETHEUR'}}
after:
{'symbol': 'ETH/EUR', 'timestamp': 1529160540908.2085, 'datetime': '2018-06-16T14:49:01.908Z',
'high': 440.45, 'low': 416.57356472, 'bid': 422.75, 'ask': 422.77,
'close': 422.76, 'last': 422.76}
答案 1 :(得分:0)
无视试图推断出你想要这样做的原因,这里是你实际设置条件的方式(用一个较小的例子):
etheurticker = {
'askVolume': 1,
'bidVolume': 2,
'vwap': 3
}
if all(x in etheurticker for x in ['askVolume', 'bidVolume', 'vwap']):
print("they are all there")
del etheurticker['vwap']
print(etheurticker)
else:
print("not all there")
# they are all there
# {'askVolume': 1, 'bidVolume': 2}
请注意,vwap
设置为None