JSON Python列表

2024-04-26 02:36:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个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"

Tags: jsonurltitlehtmpaginasinstrumentosmueblesmusicales
2条回答

如果要将值提取到新列表:

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..

你不需要对你的数据结构做任何事情。你知道吗

paginas = mydata[0]
paginas["url"].append(new_url)
paginas["title"].append(new_title)

当然,您仍然需要使用json.dump来保存它。你知道吗

相关问题 更多 >