我是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的内容,但我的想法只是例如:
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的每个数据
答案 0 :(得分:5)
json_data = [] # your list with json objects (dicts)
for item in json_data:
for data_item in item['data']:
print data_item['name'], data_item['value']
答案 1 :(得分:0)
像
这样的东西for key, value in json_data.iteritems():
print key
if isinstance(value, (list, tuple)):
for item in value:
print item
if isinstance(value, (dict)):
for value_key,value_value in value.iteritems():
print value_key,str(value_value)
可以改进以管理更多类型,并且可以递归。