JSON Python Lists



我有这个json文件

[{"url": ["instrumentos-musicales-126030594.htm", "liquidacion-muebles-y-electrodomesticos-127660457.htm"], "title": ["INSTRUMENTOS MUSICALES", "LIQUIDACION,  MUEBLES Y ELECTRODOMESTICOS"]}] 
mydata = json.load(json_data)

然后我想操作mydata将url-title对附加到列表paginas

在Python中有什么好的解决方案?我希望能够有像

这样的东西
paginas[0].url
instrumentos-musicales-126030594.htm
paginas[0].title
"INSTRUMENTOS MUSICALES"
paginas[1].url
"liquidacion-muebles-y-electrodomesticos-127660457.htm"
paginas[1].title
"LIQUIDACION,  MUEBLES Y ELECTRODOMESTICOS"
paginas = mydata[0]
paginas["url"].append(new_url)
paginas["title"].append(new_title)
当然,您仍然需要使用json.dump来保存它。

如果您想提取值到一个新的列表:

new_list =[mydata[0]["url"],mydata[0]["title"]]

将给你:

[['instrumentos-musicales-126030594.htm', 'liquidacion-muebles-y-electrodomesticos-127660457.htm'], ['INSTRUMENTOS MUSICALES', 'LIQUIDACION,  MUEBLES Y ELECTRODOMESTICOS']]

访问可以使用索引的元素:

`mydata[0]["url"][0]` will give you "instrumentos-musicales-126030594.htm"
 mydata[0]["title"][0] will give you "INSTRUMENTOS MUSICALES"  etc..
你不需要对你的数据结构做任何改动。

最新更新