python对xml文件操作小例程

来源:互联网 发布:mac玩美服lol加速器 编辑:程序博客网 时间:2024/05/21 16:59
把数据写入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(0len(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"
0 0
原创粉丝点击