python对xml文件操作小例程
来源:互联网 发布:mac玩美服lol加速器 编辑:程序博客网 时间:2024/05/21 16:59
把数据写入XML文件中: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
从XML文件读取数据: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
Python Code
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"
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"
Python Code
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"
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
- python对xml文件操作小例程
- python对xml文件写入操作
- python对XML 操作
- python对XML 操作
- python对XML 操作
- python对XML 操作
- python对XML 操作
- python对XML 操作
- Python 小程序,对文件操作及其他
- Python对xml的操作
- Python对XML进行操作
- [python]python操作xml文件
- 对 Xml 文件的操作
- XPath 对xml文件操作
- VC++对XML文件操作
- PHP对XML文件操作
- 对XMl文件的操作
- python操作XML文件------elementtree
- 计算机网络(三)
- 枚举算法
- rsyslog的relp转发及自定义端口号问题
- 2017 字符串统计
- 《python源码剖析》笔记 pythonm内存管理机制
- python对xml文件操作小例程
- error:the kernel you are installing for is a xen kernel!
- maven 转载
- android 异步处理采用Thread + Handler + Message
- 2018 母牛的故事
- Linux -- Crond 定时任务
- Play 标准的应用目录布局
- Linux虚拟文件系统
- volatile线程不安全例子