import json # 读取文本文件 file_path = 'C:/Users/andin/Desktop/wangsu.txt' output_json_path = 'C:/Users/andin/Desktop/output.json' # 初始化一个空列表来存储解析后的数据 data_list = [] # 逐行读取文本文件 with open(file_path, 'r', encoding='utf-8') as file: for line in file: parts = line.strip().split() if len(parts) >= 3: number = parts[0] location_operator = parts[1] province = parts[2] data = { 'number': number, 'location_operator': location_operator, 'province': province } data_list.append(data) # 将数据写入 JSON 文件 with open(output_json_path, 'w', encoding='utf-8') as json_file: json.dump(data_list, json_file, ensure_ascii=False, indent=4) print(f"JSON 文件已成功生成: {output_json_path}")