在Python中通过两个带有多个循环的JSON Payload进行迭代-根据结果创建字典



各位Pythoineers,我对这个输出有点困惑目标-我有两个循环,通过两个JSON有效负载进行迭代。

  • 1st Loop将所有部门密钥分组到一个唯一值列表中
  • 第二个循环遍历键(它应该与从第一个循环中收集的键相匹配,然后为该键分配一个显示名称和一个字典的键

示例第一个循环返回密钥SO第二个循环查找SO并确定displayName为SOCIOLOGYdict应返回SO: SOCIOLOGY

import json
all_courses = open('methodist_all_courses.json')
all_courses_json = json.load(all_courses)
all_departments = open('departments_payload.json')
all_departments_json = json.load(all_departments)
departments =[];
department_long = dict()
for cd_crs_id, course_details in all_courses_json.items():
for department_code in course_details['departments']:
if department_code not in departments:
departments.append(department_code)

for depot_code, departmnt_details in all_courses_json.items():
for distilled_department_code in departments:
if depot_code == distilled_department_code:
department_long[distilled_department_code] = departmnt_details['displayName']
print(department_long)

回复:https://replit.com/join/tafkwzqaya-terry-brooksjr

环路1的有效载荷:https://pastebin.com/9Bu0XL3Y回路2的有效载荷:https://pastebin.com/s0kMSmaF

输出

Traceback (most recent call last):
File "main.py", line 7, in <module>
all_departments_json = json.load(all_departments)
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/json/__init__.py", line 293, in load
return loads(fp.read(),
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/json/decoder.py", line 340, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 879 column 2 (char 25384)

departments_payload.json不是一个有效的json文件,因为它以一个额外的字符结尾。

}

删除结尾的(或者如果你从网站上提取它,只需使用切片,不包括最后一个字符。

其次,当您通过all_courses_json迭代两个循环时,您的代码不会给您所需的输出。

不过,一旦修复了json,这将起作用。

代码:

import json
all_courses = open('methodist_all_courses.json')
all_courses_json = json.load(all_courses)
all_departments = open('departments_payload.json')
all_departments_json = json.load(all_departments)

departments =[];
department_long = dict()
for cd_crs_id, course_details in all_courses_json.items():
for department_code in course_details['departments']:
if department_code not in departments:
departments.append(department_code)

for depot_code, departmnt_details in all_departments_json.items():
for distilled_department_code in departments:
if depot_code == distilled_department_code:
department_long[distilled_department_code] = departmnt_details['displayName']
print(department_long)

输出:

print(department_long)
{'AC': 'Accounting', 'AR': 'Art', 'AT': 'Athletic Training', 'BI': 'Biology', 'BU': 'Business Administration', 'CH': 'Chemistry and Physical Science', 'CM': 'Communication', 'CS': 'Computer Science', 'EC': 'Economics', 'ED': 'Education', 'EG': 'Engineering', 'LL': 'English,Literature, Languages, and Culture', 'EV': 'Environmental Management', 'HC': 'Health Care Administration', 'HI': 'History', 'HO': 'Honors', 'ID': 'Inderdisciplinary Studies', 'JU': 'Justice Studies', 'KI': 'Kinesiology', 'MK': 'Marketing', 'MB': 'Masters of Business Admninistration', 'ME': 'Masters of Education', 'MA': 'Mathematics', 'MI': 'Military Science', 'NU': 'Nursing', 'OT': 'Occupational Therapy', 'PF': 'Performing Arts', 'PH': 'Philosophy and Religion', 'DP': 'Physical Therapy', 'PA': 'Physician Assistant', 'GS': 'Political Science', 'PG': 'Professional Golf Management', 'PT': 'Professional Tennis Management', 'PS': 'Psychology', 'RM': 'Resort Management', 'SW': 'Social Work', 'SM': 'Sport Management', 'TE': 'Teacher Education'}

最新更新