我有一个JSON对象:
zt123型
zt3653型
zt777…等
我已经试过了,但我想这会让事情变得复杂。有没有简化的方法?在
def extract(dict_in, dict_out):
for key, value in dict_in.iteritems():
if isinstance(value, dict): # If value itself is dictionary
extract(value, dict_out)
elif isinstance(value, unicode):
# Write to dict_out
dict_out[key] = value
return dict_out
在这个StackOverFlow问题上选择的答案可能会对您有所帮助: What is the best (idiomatic) way to check the type of a Python variable?
这里有一个一般的方法来做这个(对于任何深度的dict)—
这基本上迭代每个键,并使用
startswith
函数来确定该键是否以zt
开头如果它在一个固定的位置,就叫这个位置:
相关问题 更多 >
编程相关推荐