Skip to content

Commit 7586c8d

Browse files
committed
Add 0017-0019
1 parent 7f69ba0 commit 7586c8d

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

Drake-Z/0017/0017.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
'''第 0017 题: 将 第 0014 题中的 student.xls 文件中的内容写到 student.xml 文件中,如
5+
下所示:
6+
<?xml version="1.0" encoding="UTF-8"?>
7+
<root>
8+
<students>
9+
<!--
10+
学生信息表
11+
"id" : [名字, 数学, 语文, 英文]
12+
-->
13+
{
14+
"1" : ["张三", 150, 120, 100],
15+
"2" : ["李四", 90, 99, 95],
16+
"3" : ["王五", 60, 66, 68]
17+
}
18+
</students>
19+
</root>'''
20+
21+
__author__ = 'Drake-Z'
22+
23+
import os
24+
import xlrd
25+
26+
def xmlwrite(hangshu):
27+
L = []
28+
L.append(r'<?xml version="1.0" encoding="UTF-8"?>')
29+
L.append(r'<root>')
30+
L.append(r'<students>')
31+
L.append(r'<!-- ')
32+
L.append(r' 学生信息表')
33+
L.append(r' "id" : [名字, 数学, 语文, 英文]')
34+
L.append(r'-->')
35+
L.append(r'{')
36+
data = xlrd.open_workbook('student.xls')
37+
table = data.sheets()[0]
38+
for i in range(0, hangshu):
39+
a = '"%s" : ["%s", %s, %s, %s],' % (table.cell(i,0).value, table.cell(i, 1).value, table.cell(i, 2).value, table.cell(i, 3).value, table.cell(i, 4).value)
40+
L.append(r' %s' % a)
41+
L.append(r'}')
42+
L.append(r'</students>')
43+
L.append(r'</root>')
44+
a = '\n'.join(L)
45+
f = open('students.xml', 'w')
46+
f.write(a)
47+
f.close()
48+
print(a)
49+
return 0
50+
51+
hangshu = 3
52+
xmlwrite(hangshu)

Drake-Z/0018/0018.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
'''第 0018 题: 将 第 0015 题中的 city.xls 文件中的内容写到 city.xml 文件中,如下所示:
5+
<?xmlversion="1.0" encoding="UTF-8"?>
6+
<root>
7+
<citys>
8+
<!--
9+
城市信息
10+
-->
11+
{
12+
"1" : "上海",
13+
"2" : "北京",
14+
"3" : "成都"
15+
}
16+
</citys>
17+
</root>'''
18+
19+
__author__ = 'Drake-Z'
20+
21+
import os
22+
import xlrd
23+
24+
def xmlwrite(file, hangshu):
25+
L = []
26+
L.append(r'<?xml version="1.0" encoding="UTF-8"?>')
27+
L.append(r'<root>')
28+
L.append(r'<citys>')
29+
L.append(r'<!-- ')
30+
L.append(r' 城市信息')
31+
L.append(r'-->')
32+
L.append(r'{')
33+
data = xlrd.open_workbook(file)
34+
table = data.sheets()[0]
35+
for i in range(0, hangshu):
36+
a = '"%s" : "%s",' % (table.cell(i, 0).value, table.cell(i, 1).value)
37+
L.append(r' %s' % a)
38+
L.append(r'}')
39+
L.append(r'</citys>')
40+
L.append(r'</root>')
41+
a = '\n'.join(L)
42+
f = open('city.xml', 'w')
43+
f.write(a)
44+
f.close()
45+
print(a)
46+
return 0
47+
48+
file = 'city.xls'
49+
hangshu = 3
50+
xmlwrite(file, hangshu)

Drake-Z/0019/0019.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
'''第 0019 题: 将 第 0016 题中的 numbers.xls 文件中的内容写到 numbers.xml 文件中,如下
5+
所示:
6+
<?xml version="1.0" encoding="UTF-8"?>
7+
<root>
8+
<numbers>
9+
<!--
10+
数字信息
11+
-->
12+
[
13+
[1, 82, 65535],
14+
[20, 90, 13],
15+
[26, 809, 1024]
16+
]
17+
</numbers>
18+
</root>'''
19+
20+
__author__ = 'Drake-Z'
21+
22+
import os
23+
import xlrd
24+
25+
def xmlwrite(file, hangshu):
26+
L = []
27+
L.append(r'<?xml version="1.0" encoding="UTF-8"?>')
28+
L.append(r'<root>')
29+
L.append(r'<numbers>')
30+
L.append(r'<!-- ')
31+
L.append(r' 数字信息')
32+
L.append(r'-->')
33+
L.append(r'[')
34+
data = xlrd.open_workbook(file)
35+
table = data.sheets()[0]
36+
for i in range(0, hangshu):
37+
a = '[%s, %s, %s],' % (table.cell(i, 0).value, table.cell(i, 1).value, table.cell(i, 2).value)
38+
L.append(r' %s' % a)
39+
L.append(r']')
40+
L.append(r'</numbers>')
41+
L.append(r'</root>')
42+
a = '\n'.join(L)
43+
f = open('numbers.xml', 'w')
44+
f.write(a)
45+
f.close()
46+
print(a)
47+
return 0
48+
49+
file = 'numbers.xls'
50+
hangshu = 3
51+
xmlwrite(file, hangshu)

0 commit comments

Comments
 (0)