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) """单选题""" def danxuan (data ): danxuan_content = "--------------------单选题--------------------\n" danxuan_num = 0 for dx in data: title = dx['name' ] options = dx['content' ] answers = dx['result' ] if answers == "0" : answer = "A" elif answers == "1" : answer = "B" elif answers == "2" : answer = "C" else : answer = "D" 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" 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' ] 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" 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" 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" 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)
```