该文章持续更新中…


Json数据解析

项目说明

项目介绍

将JSON格式的文件用python解析整理后导出项目

项目备注

  • 环境: Python3.7

  • 发布时间: 2021-11-29

项目代码

下载项目

Python整理解析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import json


"""读取题目信息"""
def read_Data(path):
with open(path,"r",encoding="utf-8") as fp:
all_data = json.loads(fp.read())

danxuan_data = all_data['data']['duanluoList'][0]['danxuan'] # 单选题
duoxuan_data = all_data['data']['duanluoList'][1]['duoxuan'] # 多选题
panduan_data = all_data['data']['duanluoList'][2]['panduan'] # 判断题
return [danxuan_data,duoxuan_data,panduan_data]

"""导出解析好的题目信息"""
def write_Data(path,data):
with open(path,"w",encoding="utf-8") as fp:
for d in data:
fp.write(d)
# print("Write Success!")


"""单选题"""
def danxuan(data):
danxuan_content = "--------------------单选题--------------------\n" # 题目&答案&选项
danxuan_num = 0 # 题序
for dx in data:
title = dx['name'] # 题目
options = dx['content'] # 选项
answers = dx['result'] # 答案

# 把正确的选项转为ABCD
if answers == "0":
answer = "A"
elif answers == "1":
answer = "B"
elif answers == "2":
answer = "C"
else:
answer = "D"

# 为选项加上ABCD
options_txt = ""
for num,option in zip(["A","B","C","D"],options):
options_txt += f"{num}.{option.lstrip()} "

# 为题目括号中加入正确答案
# 题目后面为()。时
if title[-2:] == ")。":
title = title[:-3] + answer + " )。"

# 题目后面为()时
elif title[-1] == ")":
title = f"{title[:-1]} {answer} )。"

# 题目后面为英文的()时
elif title[-1] == ")":
title = f"{title[:-1]}{answer} )。"

# 题目后面没有括号时
else:
title = f"{title}{answer} )。"

danxuan_num += 1
danxuan_content += f"{danxuan_num}. {title}\n{options_txt}\n\n"
# print(danxuan_content)
return danxuan_content


"""多选题"""
def duoxuan(data):
duoxuan_content = "--------------------多选题--------------------\n" # 题目&答案&选项
duoxuan_num = 0 # 题序
for dx in data:
title = dx['name'] # 题目
options = dx['content'] # 选项
answers = dx['result'] # 答案

# 把正确的选项转为ABCD
answer = "" # 正确答案
answers.sort() # 对列表里的数字先进行排序
for ans in answers:
if ans == "0":
answer += "A"
elif ans == "1":
answer += "B"
elif ans == "2":
answer += "C"
else:
answer += "D"

# 为选项加上ABCD
options_txt = ""
for num, option in zip(["A", "B", "C", "D"], options):
options_txt += f"{num}.{option.lstrip()} "

# 为题目括号中加入正确答案
# 题目后面为()。时
if title[-2:] == ")。":
title = title[:-3] + answer + " )。"

# 题目后面为()时
elif title[-1] == ")":
title = f"{title[:-1]} {answer} )。"

# 题目后面为英文的()时
elif title[-1] == ")":
title = f"{title[:-1]}{answer} )。"

# 题目后面没有括号时
else:
title = f"{title}{answer} )。"

duoxuan_num += 1
duoxuan_content += f"{duoxuan_num}. {title}\n{options_txt}\n\n"
# print(duoxuan_content)
return duoxuan_content

"""判断题"""
def panduan(data):
panduan_content = "--------------------判断题--------------------\n" # 题目&答案
panduan_num = 0 # 题序
for pd in data:
title = pd['name'] # 题目
answers = pd['result'] # 答案

# 转换选项
if answers == "0":
answer = "错"
else:
answer = "对"

# 填充答案
title = f"{title}{answer} )。"

# 填充题序
panduan_num += 1
panduan_content += f"{panduan_num}. {title}\n\n"
# print(panduan_content)
return panduan_content


def main(in_path,out_path):

data_list = read_Data(in_path) # 读取文件
danx_data = danxuan(data_list[0]) # 单选题
duox_data = duoxuan(data_list[1]) # 多选题
pand_data = panduan(data_list[2]) # 判断题

# 保存数据
try:
write_Data(out_path,[danx_data,duox_data,pand_data])
print("OutFile Success!")
except:
print("OutFile Fail!")

if __name__ == "__main__":

input_path = "./data.json" # 读取的文件路径
output_path = "./data.txt" # 导出的文件路径
main(input_path,output_path)

utput_path)

```