在日常工作中,我们经常需要处理各种格式的数据。有时候,我们可能需要将一个文本文件中的内容转换为 JSON 格式的数据。为了方便起见,我写了一个 Python 函数来实现这个功能。
函数功能
这个函数的主要功能是读取一个文本文件,解析其中的题目、选项、答案和解析等内容,并将其转换为 JSON 格式的数据。
实现思路
这个函数的实现思路如下:
- 首先,打开指定的文本文件,并逐行读取文件内容。
- 使用一些标志变量来跟踪当前正在处理的题目、选项、答案和解析等内容。
- 遍历文件的每一行,根据不同的情况进行处理:
- 如果是题目的开始,则提取题目的编号和内容。
- 如果是选项,则将选项内容添加到选项列表中。
- 如果是答案,则提取答案内容。
- 如果是解析,则提取解析内容。
- 如果是空行,则表示当前题目处理完毕,将其转换为 JSON 格式的数据,并添加到结果列表中。
- 最后,将结果列表转换为 JSON 格式的字符串,并将其写入到输出文件中。
示例代码
import json def txt_to_json(txt_file_path): json_data = [] with open(txt_file_path, 'r', encoding='utf-8') as file: if_analysis = 0 option_num = 0 if_ques = 1 number_ques = 1 question = '' options = [] analysis = '' data = {} answer = '' for line in file: if line.strip().split('、')[0] == str(number_ques): if_analysis = 0 option_num = 0 if_ques = 1 if_ans = 0 options = [] analysis = '' data = {} answer = '' data['id'] = number_ques question = '题目' + str(number_ques) + ':' + line.strip().split('、')[1] if len(line) >= 4 and (line[len(line)-5] + line[len(line)-4] + line[len(line)-3] + line[len(line)-2] == '( )' or line[len(line)-5] + line[len(line)-4] + line[len(line)-3] + line[len(line)-2] == ' )。' or (line[len(line)-2] == ':' and line[0] != 'A') or (line[len(line)-2] == '。' and if_ques == 1) or ('( )' in line)): if_ques = 0 if line.strip().split('、')[0] != str(number_ques): question = question + line.strip().split('\n')[0] if if_ques == 1 and line.strip().split('、')[0] != str(number_ques): question = question + line.strip().split('\n')[0] if line.strip().split(':')[0] == '答案': if_ans = 1 answer = line.strip().split(':')[1] if_analysis = 1 if if_ques == 0 and if_ans == 0 and len(line) >= 4 and (line[len(line)-5] + line[len(line)-4] + line[len(line)-3] + line[len(line)-2] != '( )' and line[len(line)-5] + line[len(line)-4] + line[len(line)-3] + line[len(line)-2] != ' )。' and (line[len(line)-2] != ':' or line[0] == 'A') and (line[len(line)-2] != '。' or if_ques == 0) and ('( )' not in line)): uppercase_alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] if line[0] in uppercase_alphabet: if line[0] == 'A': str_join = ' '.join(options) str_earn = '' print(str_join) for i in range(0,len(options)): print(type(options[i])) str_earn = str_earn + options[i].strip().split('、')[1] # if char_join[i] not in uppercase_alphabet: # str_earn = str_earn + char_join[i] question = question + str_earn print(question) options = [] print('hjx') line1 = line.strip().split('\n')[0] options.append(line1) option_num = option_num + 1 else: line1 = line.strip().split('\n')[0] options.append(uppercase_alphabet[option_num]+'、'+line1) option_num = option_num + 1 if if_analysis == 1 and line.strip().split(':')[0] != '答案': analysis = analysis + line.strip().split('\n')[0] if line == '\n': number_ques = number_ques + 1 data['question'] = question data['type'] = "选择题" data['options'] = options data['answer'] = answer if analysis: data['analysis'] = analysis json_data.append(data) print(json_data) json_data = json.dumps(json_data, indent=4, ensure_ascii=False) with open('output.json', 'w', encoding='utf-8') as output_file: # print(json_data) # output_file.write("questions = " + json_data) output_file.write('"questions":' + json_data) txt_to_json('python基础题库.txt')
复制
结论
通过这个函数,我们可以轻松地将一个文本文件中的内容转换为 JSON 格式的数据,这样就可以更方便地进行后续的处理和分析了。