python读文件保存到字典,修改字典并写入新文件的实例

作者:beboydavid 时间:2021-09-19 05:37:57 

实例如下所示:


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 key in lkeys:
value=tcode.get(key)
key=key.upper().replace("0X","0x")
templine.append("%s\t%s\n"%(key,value))
with open('text_trans.txt','w+') as filetwo:
filetwo.writelines(templine)
if __name__ == '__main__':
GetTcode()
GetTransCode()
ReplaTransCode()

来源:https://blog.csdn.net/beboydavid/article/details/72903378

标签:python,字典,写入,文件
0
投稿

猜你喜欢

  • Python线程协作threading.Condition实现过程解析

    2023-07-25 11:15:27
  • antd项目实现彩蛋效果的详细代码

    2023-09-14 12:51:20
  • 带你深入了解数据库设计中的英文术语表

    2008-12-09 14:53:00
  • Python使用psutil获取进程信息的例子

    2021-11-03 07:29:47
  • AJAX打造博客无刷新搜索

    2007-08-23 08:48:00
  • Pycharm挂代理后依旧插件下载慢的完美解决方法

    2023-06-17 23:07:31
  • JavaScript 中的 Base64 编码(一):Encode

    2010-07-23 10:16:00
  • MySQL字段类型说明

    2007-09-27 19:22:00
  • PyQt5按下按键选择文件夹并显示的实现

    2023-05-02 02:02:10
  • python实现图像边缘检测

    2022-03-17 15:35:11
  • PHP PDOStatement::setAttribute讲解

    2023-06-04 02:48:47
  • java与php的区别浅析

    2023-11-14 23:27:23
  • 如何在mac环境中用python处理protobuf

    2021-02-26 08:42:06
  • 我跟iframe之间的误会

    2008-03-17 13:30:00
  • asp无组件上传并插入到数据库里

    2008-10-24 10:04:00
  • Python处理中文标点符号大集合

    2021-12-07 16:03:45
  • Python基础教程之循环语句(for、while和嵌套循环)

    2022-07-30 10:00:26
  • PHP信号处理机制的操作代码讲解

    2023-07-14 09:26:49
  • python用pickle模块实现“增删改查”的简易功能

    2022-01-13 06:38:56
  • 关于Python dict存中文字符dumps()的问题

    2022-08-22 17:53:17
  • asp之家 网络编程 m.aspxhome.com