python对xml文件操作小例程

把数据写入XML文件中:
 Python Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#!/usr/bin/python

import  sys
import  xml.etree.ElementTree  as  ET

def  main():
    
global  filename
    
print   "please input follow book message(use TAB to divide):"
    
print   "book-name    book-author     publish-date"
    book_data = ET.Element(
'data' )
    
while   1 :
        book_msg = 
raw_input ( "" )
        
if  book_msg ==  "quit" :
            
break
        bk = book_msg.
split ( "\t" )
        bk_2 = []
        
for  i  in   range ( 0 len (bk)):
            
if  bk[i] !=  "" :
                bk_2.
append (bk[i])
        book_msg         = ET.SubElement(book_data, 
'book_msg' )
        book_msg.
set ( "publish_date" , bk_2[ 2 ]. decode ( "utf-8" ))
        book_name        = ET.SubElement(book_msg, 
"name" )
        book_name.text   = bk_2[
0 ]. decode ( "utf-8" )
        book_author      = ET.SubElement(book_msg, 
"author" )
        book_author.text = bk_2[
1 ]. decode ( "utf-8" )
#       book_date        = ET.SubElement(book_msg, "publish_date")
#       book_date.text   = bk_2[2].decode("utf-8")
    xml_string = ET.tostring(book_data, encoding= "utf-8" , method= "xml" )
    fp = 
file (filename,  "w" )
    fp.
write ( "<?xml version=\"1.0\"?>\n" )
    fp.
write (xml_string)
    fp.
close ()

if  __name__ ==  '__main__' :
    filename = sys.argv[
1 ]
    main()
else :
    
print   "Module be imported"
从XML文件读取数据:
 Python Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

#!/usr/bin/python

import  xml.etree.ElementTree  as  ET

def  main():
    tree = ET.parse(
"./book.xml" )
    root = tree.getroot()
    
print   "book message:"
    
for  child  in  root.findall( "book_msg" ):
        
print   "name:%s\tauthor:%s\tpublish date:%s\n"  % \
(child.
find ( "name" ).text, child. find ( "author" ).text, \
child.
get ( "publish_date" )),
    
print   ""
    
while   1 :
        
print   "please select which to search:"
        
print   "1. search book name"
        
print   "2. search book author"
        
print   "3. search book publish date"
        choice = 
int ( raw_input ( "Choose:" ))
        
if  choice ==  1 :
            bk_name = 
raw_input ( "Input book name:" )
            flag = 
1
            
for  child  in  root.findall( "book_msg" ):
                
if  child. find ( "name" ).text. find (bk_name. decode ( "utf-8" )) != - 1 :
                    flag = 
0
                    
print   "book message: name:%s author:%s publish date:%s\n"  % \
(child.
find ( "name" ).text, child. find ( "author" ).text, child. get ( "publish_date" ))
            
if  flag ==  1 :
                
print   "the book (%s) is not exist."  % bk_name
        
elif  choice ==  2 :
            bk_author = 
raw_input ( "Input book author:" )
            flag = 
1
            
for  child  in  root.findall( "book_msg" ):
                
if  child. find ( "author" ).text. find (bk_author. decode ( "utf-8" )) != - 1 :
                    flag = 
0
                    
print   "book message: name:%s author:%s publish date:%s\n"  % \
(child.
find ( "name" ).text, child. find ( "author" ).text, child. get ( "publish_date" ))
            
if  flag ==  1 :
                
print   "the book writen by (%s) is not exist."  % bk_author
        
elif  choice ==  3 :
            bk_publish_date = 
raw_input ( "Input book publish date:" )
            flag = 
1
            
for  child  in  root.findall( "book_msg" ):
#               if child.find("publish_date").text.find(bk_publish_date.decode("utf-8")) != -1:
                 if  child. get ( "publish_date" ). find (bk_publish_date. decode ( "utf-8" )) \
!= -
1 :
                    flag = 
0
                    
print   "book message: name:%s author:%s publish date:%s\n"  % \
(child.
find ( "name" ).text, child. find ( "author" ).text, child. get ( "publish_date" ))
            
if  flag ==  1 :
                
print   "the book writen at (%s) is not exist."  % bk_publish_date
        
else :
            
print   "wrong choice, please choose again."

if  __name__ ==  '__main__' :
    main()
else :
    
print   "Module be imported"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值