gpt4 book ai didi

python - 如何使用python从列表Json中获取数据

转载 作者:太空狗 更新时间:2023-10-29 21:49:47 24 4
gpt4 key购买 nike

我是 python 的新手,并尝试从 python json 文档中获取数据,我尝试做的是将信息传递给 python 和 json 从 python 打印一个带有表格样式的 pdf。我在 json 中的代码是

[
{
"files": 0,
"data": [
{"name": "RFC", "value": "XXXXXXX", "attId": 01},
{"name": "NOMBRE", "value": "JOSE", "attId": 02},
{"name": "APELLIDO PATERNO", "value": "MONTIEL", "attId": 03},
{"name": "APELLIDO MATERNO", "value": "MENDOZA", "attId": 04},
{"name": "FECHA NACIMIENTO", "value": "1989-02-04", "attId": 05}
],
"dirId": 1,
"docId": 4,
"structure": {
"name": "personales",
"folioId": 22
}
},
{
"files": 0,
"data": [
{"name": "CALLE", "value": "AMOR", "attId": 06},
{"name": "No. EXTERIOR", "value": "4", "attId": 07},
{"name": "No. INTERIOR", "value": "2", "attId": 08},
{"name": "C.P.", "value": "55060", "attId": 09},
{"name": "ENTIDAD", "value": "ESTADO DE MEXICO", "attId": 10},
{"name": "MUNICIPIO", "value": "ECATEPEC", "attId": 11},
{"name": "COLONIA", "value": "INDUSTRIAL", "attId": 12}
],
"dirId": 1,
"docId": 4,
"structure": {
"name": "direccion",
"folioId": 22
}
}
]

在 python 中我提示下一个代码

import json
f= open(prueba.json)
prueba = json.load(f)
prueba

正确打印json的内容,但是我的想法是get only example:

Nombre,Jose

然后使用参数建表成pdf

我试过以下方法

 import json
json_data = []
with open('prueba.json') as json_file:
json_data = json.load(json_file)
for key, value in json_data.iteritems():
print key;
for item in value:
print item
for key, value in json_data.iteritems():
print key;
for item in value:
print item

但是我有下一个错误:

    AttributeError : 'list' object has no attribute 'iteritems'

我正在尝试为他们做点什么,但我必须获取 json 的每个数据

最佳答案

json_data = [] # your list with json objects (dicts)

with open('prueba.json') as json_file:
json_data = json.load(json_file)

for item in json_data:
for data_item in item['data']:
print data_item['name'], data_item['value']

关于python - 如何使用python从列表Json中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30510352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com