Python编程将多个字典文件合并成一个字典文件,代码如下:
//例子:合并a.txt和b.txt两个字典文件def?readf(filename):
lines?=?file(filename).readlines()
dic?=?{}
for?i?in?lines:
i_?=?i.split()
dic[i_[0]]?=?int(i_[1])
return?dic
dica?=?readf('a.txt')
dicb?=?readf('b.txt')
lines?=?[]
for?i?in?dica:
percent?=?str(float(dicb[i])*100/dica[i])+'%'
s?=?'?'.join([i,?str(dica[i]),?str(dicb[i]),?percent])
s?+=?'\n'
lines.append(s)
//合并成一个字典文件c.txt?
with?open('c.txt',?'w')?as?f:
f.writelines(lines)
f.close()