检索数组

时间:2018-05-30 21:05:13

标签: python json python-3.x dictionary

这是我正在使用的文本文件的内容:

[{"name": "Bitcoin", "symbol": "BTC", "rank": 1, "slug": "bitcoin", "tokens": ["Bitcoin", "bitcoin", "BTC"], "id": 1},
 {"name": "Ethereum", "symbol": "ETH", "rank": 2, "slug": "ethereum", "tokens": ["Ethereum", "ethereum", "ETH"], "id": 1027}, ... ]

如何检索每个"名称"每个硬币的元素并将其保存为临时变量?

2 个答案:

答案 0 :(得分:2)

看起来像一个有效的JSON,所以只需使用内置的json模块来解析它,例如:

int

答案 1 :(得分:1)

如果您习惯使用第三方库,class Pizza attr_reader :topping # I had to add the name in the initializer for better error reporting. Toppings = Enum.new('Toppings') do option(:pepperoni).set("Pepperoni") option(:sausage).set("Sausage") option(:mushrooms).set("Mushrooms") end def set_topping(symbol) @topping = Toppings[symbol] end end pizza = Pizza.new ### Happy Case pizza.set_topping(:pepperoni) #=> "Pepperoni" ### Sad Case (Error message shown below is what I'm trying to figure out) pizza.set_topping(:spinach) #=> RuntimeError: Option Toppings::spinach not found. 会直接接受词典列表:

pandas