处理前文件内容 代码 处理后的 # 读取代码 fr = open('three.txt', 'r') dic = {} keys = [] # 用来存储读取的顺序 for line in fr: v = line.strip().split(':') dic[v[0]] = v[1] keys.append(v[0]) fr.close() print(dic) # # 写入文件代码 通过keys的顺序写入 # fw = open('wdic.txt', 'w') # for k in keys: # fw.write(k + ':' + dic[k] + '\n') #