如何将.Txt文档中的String转换为Dictionary Vairable

时间:2017-05-26 08:58:26

标签: python python-3.x

import json

h = '{1":{"Name":"Teddies","Amount":6,"PPU":int(6),"CPU":int(4)},2:{"Name":"WoodenToys","Amount":0,"PPU":int(15) * int(1),"CPU":int(10)}}'

d = json.loads(h)

print(d[1]["Name"])

1 个答案:

答案 0 :(得分:0)

看起来你在dict定义的开头有一个拼写错误。 正确的语法应该是:barplot(t(as.matrix(prey[, c("weasels", "voles")])), axisnames=FALSE,names.arg=NULL,yaxt= "n",beside = TRUE) axis(1, cex.axis=0.8, at = c(2,5,8,11,14,17,20,23,26),line=0.4,labels = c("2003","2004","2005","2006","2007","2008","2014","2015","2016"), 2) axis(2, cex.axis=0.8,at = c(0, 5,10,15,20,25,30,35,40),labels = round(c(0, 5,10,15,20,25,30,35,40)), 2) axis(4, cex.axis=0.8,at = c(0, 5,10,15,20,25,30),labels = round(c(0, 5,10,15,20,25,30)), 2)

您可以使用h = '{"1":{"Name":"Teddies","Amount":6,"PPU":int(6),"CPU":int(4)},2:{"Name":"WoodenToys","Amount":0,"PPU":int(15) * int(1),"CPU": int(10)}}'功能,但请注意,因为它不安全,因此不推荐。

请查看ast模块中的eval()功能。

您将不得不失去literal_eval()解析或以某种方式执行...