树状转XML

将程序日志中的树状结构的报文,转换为XML格式的报文。

输入样式为

 1 ├─0.COMMON_INFO(utype)
 2 │  ├─0.PROVINCE_GROUP(string):10017
 3 │  ├─1.SESSION_ID(string):Dj32QgQQlzjD2MFRn1JKcLlffXL6xfFRKn313Bnyr8SwfGV12dQS!903836304!1348468960414
 4 │  ├─2.SER_NAME(string):
 5 │  ├─3.ROUND_AUDIT(string):Y
 6 │  ├─4.DEST_IP(string):172.21.0.123
 7 │  ├─5.DEST_PORT(string):11001
 8 │  ├─6.SRC_PORT(string):56284
 9 │  ├─7.APP_NAME(string):NGBOSS
10 │  ├─8.APP_ID(string):2000
11 │  └─9.FROM_SYS(string):BOSS
12 ├─1.BILL_TYPE(int):1
13 ├─2.START_TIME(string):20120903000000
14 ├─3.END_TIME(string):20120924235959
15 ├─4.ELECTRIC_FLAG(string):N
16 ├─5.LOGIN_NO(string):M00000SYS
17 └─6.PAGINATION_INFO(utype)
18     ├─0.LINE_COUNT(int):20
19     └─1.PAGE_NUM(int):1

输出样式为

 1 <COMMON_INFO>
 2     <PROVINCE_GROUP type="string">10017</PROVINCE_GROUP>
 3     <SESSION_ID type="string">Dj32QgQQlzjD2MFRn1JKcLlffXL6xfFRKn313Bnyr8SwfGV12dQS!903836304!1348468960414</SESSION_ID>
 4     <SER_NAME type="string"></SER_NAME>
 5     <ROUND_AUDIT type="string">Y</ROUND_AUDIT>
 6     <DEST_IP type="string">172.21.0.123</DEST_IP>
 7     <DEST_PORT type="string">11001</DEST_PORT>
 8     <SRC_PORT type="string">56284</SRC_PORT>
 9     <APP_NAME type="string">NGBOSS</APP_NAME>
10     <APP_ID type="string">2000</APP_ID>
11     <FROM_SYS type="string">BOSS</FROM_SYS>
12 </COMMON_INFO>
13 <BILL_TYPE type="int">1</BILL_TYPE>
14 <START_TIME type="string">20120903000000</START_TIME>
15 <END_TIME type="string">20120924235959</END_TIME>
16 <ELECTRIC_FLAG type="string">N</ELECTRIC_FLAG>
17 <LOGIN_NO type="string">M00000SYS</LOGIN_NO>
18 <PAGINATION_INFO>
19     <LINE_COUNT type="int">20</LINE_COUNT>
20     <PAGE_NUM type="int">1</PAGE_NUM>
21 </PAGINATION_INFO>

 

 

python代码

 1 # -*- coding:gb2312 -*-
 2 # To change this template, choose Tools | Templates
 3 # and open the template in the editor.
 4 
 5 __author__="Shawn"
 6 __date__ ="$2012-11-18 15:28:40$"
 7 
 8 
 9 if __name__ == "__main__":
10     curr_line=""
11     pre_level=0
12     f=open("in.txt","r")
13     p=open("out.txt","w")
14     alist=[]
15     alist_level=[]
16 
17     for eachLine in f:
18         curr_line=eachLine
19         dot_index=curr_line.index(".")
20         left_quote_index=curr_line.index("(")
21         right_quote_index=curr_line.index(")")
22         level_index=0
23         if(curr_line.find("")!=-1):
24             level_index=curr_line.index("")
25         if(curr_line.find("")!=-1):
26             level_index=curr_line.index("")
27         blank_num=curr_line.count(" ", 0, dot_index)
28         level=int((level_index-blank_num/2)/2)
29         if(level<pre_level):
30             begin_level=level
31             for i in range(level,pre_level):
32                 begin_level=begin_level-1
33                 buffer="    "*alist_level.pop()+"</"+alist.pop()+">"
34                 p.writelines(buffer+'\n')
35         type=curr_line[left_quote_index+1:right_quote_index]
36         node_name=curr_line[dot_index+1:left_quote_index]
37         if(type!="utype"):
38             node_value=curr_line[curr_line.index(":")+1:]
39             buffer="    "*level+"<"+node_name+" type=\""+type+"\">"+node_value.rstrip()+"</"+node_name+">"
40             p.writelines(buffer+'\n')
41         else:
42             buffer="    "*level+"<"+node_name+">"
43             p.writelines(buffer+'\n')
44             alist.append(node_name)
45             alist_level.append(level)
46             node_value=""
47         pre_level=level
48     while(alist.__len__()!=0):
49         buffer="    "*alist_level.pop()+"</"+alist.pop()+">"
50         p.writelines(buffer+'\n')
51     f.close()
52     p.close()

 

posted @ 2012-12-05 17:41  黄牛  阅读(163)  评论(0编辑  收藏  举报