欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python讀文件保存到字典,修改字典并寫入新文件的實例

 更新時間:2018年04月23日 09:41:43   作者:beboydavid  
下面小編就為大家分享一篇python讀文件保存到字典,修改字典并寫入新文件的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

實例如下所示:

tcode={}
transcode={}
def GetTcode():
#從文本中獲取英文對應(yīng)的故障碼,并保存在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():
#從文本中獲取中文對應(yīng)的故障碼,并保存在tcode字典(故障碼文本樣例:oxff,0xff,0x00,0x01,	"燃油調(diào)節(jié)器控制電路過高")
	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():
#將已經(jīng)翻譯的中文故障碼在英文文本中用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()

以上這篇python讀文件保存到字典,修改字典并寫入新文件的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • pytorch 運行一段時間后出現(xiàn)GPU OOM的問題

    pytorch 運行一段時間后出現(xiàn)GPU OOM的問題

    這篇文章主要介紹了pytorch 運行一段時間后出現(xiàn)GPU OOM的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • PyTorch中的方法torch.randperm()示例介紹

    PyTorch中的方法torch.randperm()示例介紹

    在 PyTorch 中,torch.randperm(n) 函數(shù)用于生成一個從 0 到 n-1 的隨機排列的整數(shù)序列,這篇文章主要介紹了PyTorch中的方法torch.randperm()介紹,需要的朋友可以參考下
    2024-05-05
  • Django多對多ManyToManyField字段的使用

    Django多對多ManyToManyField字段的使用

    Django是一個支持多對多關(guān)系的Web框架,可以在模型中定義多對多關(guān)系,本文主要介紹了Django多對多ManyToManyField字段的使用,感興趣的可以了解一下
    2023-12-12
  • Python實現(xiàn)動態(tài)繪圖的示例詳解

    Python實現(xiàn)動態(tài)繪圖的示例詳解

    matplotlib中的animation提供了動態(tài)繪圖功能,這篇文章主要為大家詳細介紹了Python如何利用matplotlib實現(xiàn)動態(tài)繪圖,感興趣的可以了解一下
    2023-05-05
  • Python如何查看并打印matplotlib中所有的colormap(cmap)類型

    Python如何查看并打印matplotlib中所有的colormap(cmap)類型

    這篇文章主要介紹了Python如何查看并打印matplotlib中所有的colormap(cmap)類型,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 最新評論