我是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'}}
希望任何人能帮助我:)
发布于 2018-06-16 23:15:30
您可以像这样删除它们:
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}
发布于 2018-06-16 23:10:54
不考虑你为什么要这样做,下面是你实际如何设置条件(用一个较小的例子):
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
并不重要
https://stackoverflow.com/questions/50888967
复制相似问题