简体   繁体   English

Python JSON for 循环仅返回最后一条记录

[英]Python JSON for loop only returns last record

Python parsing JSON file that is a list with an embedded dictionary I can get the values from it but only for the last record in the dictionary Python 解析 JSON 文件,这是一个带有嵌入字典的列表我可以从中获取值,但仅适用于字典中的最后一条记录

Code is attached on what I've tried代码附在我尝试过的内容上

import json

access_json = open('shortdocuments.json', 'r')
read_content = json.load(access_json)
# Make list a dictionary
for question_access in read_content:
    print(type(question_access))

replies_access = question_access['Attachment']


for i in replies_access:
    print(i,"|",replies_access[i])

Wanting out put like this for all records想把所有记录都这样放

CreateDate | 2019-10-16T09:13:33

Description |
CreateUserID | 1

Path | 201910\10489_AParker_T0231056_13.pdf

Name | 10489_AParker_T0231056_13
FileID | 765

IsDepotUsed | True

Extension | .pdf

UpdateDate | 2019-10-16T09:13:33

UpdateUserID | 1

My JSON FIle我的 JSON 文件

[
  {
    "UserDefinedValueID": 872,
    "UserDefinedFieldID": 56,
    "ParentID": 355,
    "Name": "PDM_Application",
    "Value": "763",
    "UpdateDate": "2019-10-27T14:29:18",
    "FieldType": "File",
    "Attachment": {
      "FileID": 763,
      "Name": "03981-00117",
      "Description": "",
      "IsDepotUsed": true,
      "Path": "201910\\03981-00117.pdf",
      "Extension": ".pdf",
      "CreateDate": "2019-10-16T09:13:32",
      "UpdateDate": "2019-10-27T14:29:18",
      "CreateUserID": 1,
      "UpdateUserID": 1
    },
    "UpdateUserID": 1,
    "CreateUserID": 1  },
  {
    "UserDefinedValueID": 873,
    "UserDefinedFieldID": 57,
    "ParentID": 355,
    "Name": "PDM_LeaseDoc",
    "Value": "764",
    "UpdateDate": "2019-10-16T09:13:33",
    "FieldType": "File",
    "Attachment": {
      "FileID": 764,
      "Name": "09658-00060_t0007192_Application",
      "Description": "",
      "IsDepotUsed": true,
      "Path": "201910\\09658-00060_t0007192_Application.pdf",
      "Extension": ".pdf",
      "CreateDate": "2019-10-16T09:13:33",
      "UpdateDate": "2019-10-16T09:13:33",
      "CreateUserID": 1,
      "UpdateUserID": 1
    },
    "UpdateUserID": 1,
    "CreateUserID": 1  },
  {
    "UserDefinedValueID": 875,
    "UserDefinedFieldID": 59,
    "ParentID": 355,
    "Name": "PDM_FAS/SODA",
    "Value": "765",
    "UpdateDate": "2019-10-16T09:13:33",
    "FieldType": "File",
    "Attachment": {
      "FileID": 765,
      "Name": "10489_AParker_T0231056_13",
      "Description": "",
      "IsDepotUsed": true,
      "Path": "201910\\10489_AParker_T0231056_13.pdf",
      "Extension": ".pdf",
      "CreateDate": "2019-10-16T09:13:33",
      "UpdateDate": "2019-10-16T09:13:33",
      "CreateUserID": 1,
      "UpdateUserID": 1
    },
    "UpdateUserID": 1,
    "CreateUserID": 1 
 }
]

Following a for-loop, the variable that you used is still in scope.在 for 循环之后,您使用的变量仍在 scope 中。

for question_access in read_content:
    print(type(question_access))

# question_access is still in scope, and the last item in the list
replies_access = question_access['Attachment']

You need to indent the code under the loop to get each item acted upon您需要在循环下缩进代码以使每个项目都被执行

for question_access in read_content:
    replies_access = question_access['Attachment']
    for i in replies_access:
        print(i,"|",replies_access[i])

Edit: If you want a CSV type format, you can try this编辑:如果你想要一个 CSV 类型格式,你可以试试这个

import json

with open('shortdocuments.json') as f:
    data = json.load(f)

if data:
    i = iter(data)
    a = next(i)['Attachment']
    print('|'.join(a.keys()))  # comment line to get only values
    while True:
        try:
            print('|'.join(map(str, a.values())))
            a = next(i)['Attachment']
        except StopIteration:
            break

Outputs输出

CreateDate|CreateUserID|Description|Extension|FileID|IsDepotUsed|Name|Path|UpdateDate|UpdateUserID
2019-10-16T09:13:32|1||.pdf|763|True|03981-00117|201910\03981-00117.pdf|2019-10-27T14:29:18|1
2019-10-16T09:13:33|1||.pdf|764|True|09658-00060_t0007192_Application|201910\09658-00060_t0007192_Application.pdf|2019-10-16T09:13:33|1
2019-10-16T09:13:33|1||.pdf|765|True|10489_AParker_T0231056_13|201910\10489_AParker_T0231056_13.pdf|2019-10-16T09:13:33|1

Or using pandas或使用 pandas

import json
from pandas import DataFrame

with open('shortdocuments.json') as f:
    data = json.load(f)
    attachments = [d['Attachment'] for d in data]
    print(DataFrame.from_dict(attachments).to_csv(sep='|', index=False))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM