这里为大家介绍一个python关联数组的应用实例,供大家学习参考。
想使用linuxjishu/13830.html target=_blank class=infotextkey>awk对一个文件进行处理,苦于没有思路,最后还是使用python实现了。
Release SPA Problem owner
289 DROUTER cr_123 jiji
2810 DROUTER cr_1234 qiqi
289 EPAY cr_234 zhaon
289 DROUTER cr_12345 qiqi
处理后的结果 :
Release : 289
SPA: DROUTER
cr_123 jiji
cr_234 zhaon
SPA:EPAY
cr_12345 qiqi
Release: 2810
SPA:DROUTER
cr_1234 qiqi
python的代码
#!/usr/bin/python
FD = open("/root/data","r")
R = {}
firline = FD.readline()
for line in FD.readlines():
tmp=line.split()
if tmp[0] in R:
if tmp[1] in R[tmp[0]]:
R[tmp[0]][tmp[1]][tmp[3]]=tmp[2]
else :
R[tmp[0]][tmp[1]]={}
R[tmp[0]][tmp[1]][tmp[3]]=tmp[2]
else:
R[tmp[0]]={}
R[tmp[0]][tmp[1]]={}
R[tmp[0]][tmp[1]][tmp[3]]=tmp[2]
for release in R.keys():
print "Release: " + release
for spa in R[release].keys():
print "tSPA: " + spa
for own in R[release][spa].keys():
print "tt" + R[release][spa][own] + "t" + own