Python Multilevel dictionary value merging:
#!/bin/env pythonimport os,sys,reimport cstringiof=open ('/tmp/3.txt ') '/tmp/3.txt content :148616 ' 192.168.0.127:8080 ' 0.157 {': 130000, ' 206 ': 250, ' 301 ': 90, ' 302 ': 6698, ' 304 ': 6018, ' 406 ': 5} 148616 ' 192.168.0.127:8080 ' 0.157 {': 130999, ' 206 ': 250, ' 301 ': 90, ' 302 ': 6698, ' 304 ': 6018, ' 406 ': 5} 100 ' 192.168.0.126:8080 ' 90.007 {': 1} "' D={}p=re.compile ([0-9]+). *\ ' ([0-9]+.[ 0-9]+. [0-9]+. [0-9]+:,?. *?) \‘.*? ([0-9]+\. [0-9]+). * ({. *}) "S=cstringio.stringio () for line in f: host_port=str (Re.match (p,line). Group (2). Split (', ') [ -1].strip ()) drespon_code=eval (Re.match (p,line). Group (4)) # drespon_200code=eval (Re.match (p,line). Group (4)) [' 200 '] d[host_port]=Drespon_code s.write (str (d) + ",") #print d d={}ret={}for dict in eval (S.getvalue (). Rstrip (', ')): for key,val in dict.iteritems (): for zkey in val.keys (): host_key=str (key+ "_" +zkey) num=ret.get ( host_key,0) num+=val[zkey] ret[host_key]=numprint ret ' {' 192.168.0.127:8080_406 ': 10, ' 192.168.0.126:8080_200 ': 1, ' 192.168.0.127:8080_200 ': 260999, ' 192.168.0.127:8080_206 ': 500, ' 192.168.0.127:8080_301 ': 180, ' 192.168.0.127:8080_302 ': 13396, ' 192.168.0.127:8080_304 ': 12036 '
This article is from the Linux system maintenance blog, so be sure to keep this source http://linuxadmin.blog.51cto.com/2683824/1658417
Python Multi-level dictionary value merging