tcode={}
transcode={}
def GetTcode():
#从文本中获取英文对应的故障码,并保存在tcode字典(故障码文本样例:oxff,0xff,0x00,0x01, "Fuel Volume Regulator Control Circuit High")
with open('text_en.txt','r+')as fileone:
for line in fileone.readlines():
if not line:
continue
line=line.strip()
titems=line.split('\t')
strkey=titems[0].lower()
strtemp=titems[1]
tcode[strkey]=strtemp
def GetTransCode():
#从文本中获取中文对应的故障码,并保存在tcode字典(故障码文本样例:oxff,0xff,0x00,0x01, "燃油调节器控制电路过高")
with open('text_cn.txt','r+') as fileone:
for line in fileone.readlines():
if not line:
continue
line=line.strip()
transcode[line.split('\t')[0].lower()]=line.split('\t')[1]
def ReplaTransCode():
#将已经翻译的中文故障码在英文文本中用ID查找出来并替换,对新的tcode字典key进行排序,并写入新的文本中
for findkey in transcode.keys():
if tcode.get(findkey,-1)!= -1:
tcode[findkey]=transcode[findkey]
templine=[]
lkeys=tcode.keys()
lkeys.sort()
for k
Python操作字典与文件:读取、转换与写入

该博客介绍了如何使用Python读取文本文件中的故障码信息,将其存储到字典中,然后对字典进行修改,并将修改后的数据写入新文件。通过`GetTcode()`函数读取英文故障码,`GetTransCode()`函数读取中文故障码,最后`ReplaTransCode()`函数进行字典替换与新文件写入。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



