Skip to content

Commit b1c4694

Browse files
committed
Merge pull request Show-Me-the-Code#110 from JiYouMCC/master
0017 0018 0019
2 parents 3f540ad + ca8adfc commit b1c4694

File tree

9 files changed

+204
-0
lines changed

9 files changed

+204
-0
lines changed

JiYouMCC/0017/0017.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -*- coding: utf-8 -*-
2+
import xlrd
3+
from xml.dom.minidom import Document
4+
from xml.etree.ElementTree import Comment, Element
5+
import json
6+
7+
infos = []
8+
info_file = xlrd.open_workbook('students.xls')
9+
info_table = info_file.sheets()[0]
10+
row_count = info_table.nrows
11+
doc = Document()
12+
root = doc.createElement('root')
13+
doc.appendChild(root)
14+
students = doc.createElement('students')
15+
for row in range(row_count):
16+
student = doc.createElement('student')
17+
student.setAttribute('name', info_table.cell(row, 1).value.encode('utf-8'))
18+
scores = doc.createElement('scores')
19+
score = doc.createElement('score')
20+
score.setAttribute('subject', '数学')
21+
score.appendChild(doc.createTextNode('%d' % info_table.cell(row, 2).value))
22+
scores.appendChild(score)
23+
score1 = doc.createElement('score')
24+
score1.setAttribute('subject', '语文')
25+
score1.appendChild(doc.createTextNode('%d' % info_table.cell(row, 3).value))
26+
scores.appendChild(score1)
27+
score2 = doc.createElement('score')
28+
score2.setAttribute('subject', '英文')
29+
score2.appendChild(doc.createTextNode('%d' % info_table.cell(row, 4).value))
30+
scores.appendChild(score2)
31+
student.appendChild(scores)
32+
students.appendChild(student)
33+
root.appendChild(students)
34+
file = open('students.xml','w')
35+
file.write(doc.toprettyxml(indent = ''))
36+
file.close()

JiYouMCC/0017/readme.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
**第 0017 题:** 将 第 0014 题中的 student.xls 文件中的内容写到 student.xml 文件中,如
2+
3+
下所示:
4+
5+
<?xml version="1.0" encoding="UTF-8"?>
6+
<root>
7+
<students>
8+
<!--
9+
学生信息表
10+
"id" : [名字, 数学, 语文, 英文]
11+
-->
12+
{
13+
"1" : ["张三", 150, 120, 100],
14+
"2" : ["李四", 90, 99, 95],
15+
"3" : ["王五", 60, 66, 68]
16+
}
17+
</students>
18+
</root>
19+
20+
- [阅读资料](http://www.cnblogs.com/skynet/archive/2013/05/06/3063245.html) 腾讯游戏开发 xml 和 Excel 相互转换
21+
----
22+
XML里面嵌套JSON,觉累不爱。

JiYouMCC/0017/students.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" ?>
2+
<root>
3+
<students>
4+
<student name="张三">
5+
<scores>
6+
<score subject="数学">150</score>
7+
<score subject="语文">120</score>
8+
<score subject="英文">100</score>
9+
</scores>
10+
</student>
11+
<student name="李四">
12+
<scores>
13+
<score subject="数学">90</score>
14+
<score subject="语文">99</score>
15+
<score subject="英文">95</score>
16+
</scores>
17+
</student>
18+
<student name="王五">
19+
<scores>
20+
<score subject="数学">60</score>
21+
<score subject="语文">66</score>
22+
<score subject="英文">68</score>
23+
</scores>
24+
</student>
25+
</students>
26+
</root>

JiYouMCC/0018/0018.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- coding: utf-8 -*-
2+
import xlrd
3+
from xml.dom.minidom import Document
4+
from xml.etree.ElementTree import Comment, Element
5+
import json
6+
7+
infos = []
8+
info_file = xlrd.open_workbook('city.xls')
9+
info_table = info_file.sheets()[0]
10+
row_count = info_table.nrows
11+
doc = Document()
12+
root = doc.createElement('root')
13+
doc.appendChild(root)
14+
citys = doc.createElement('citys')
15+
for row in range(row_count):
16+
city = doc.createElement('city')
17+
city.setAttribute('id', '%d' % info_table.cell(row, 0).value)
18+
city.appendChild(
19+
doc.createTextNode('%s' % info_table.cell(row, 1).value.encode('utf-8')))
20+
citys.appendChild(city)
21+
root.appendChild(citys)
22+
file = open('citys.xml', 'w')
23+
file.write(doc.toprettyxml(indent=''))
24+
file.close()

JiYouMCC/0018/citys.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" ?>
2+
<root>
3+
<citys>
4+
<city id="1">上海</city>
5+
<city id="2">北京</city>
6+
<city id="3">成都</city>
7+
</citys>
8+
</root>

JiYouMCC/0018/readme.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
**第 0018 题:** 将 第 0015 题中的 city.xls 文件中的内容写到 city.xml 文件中,如下所示:
2+
3+
<?xmlversion="1.0" encoding="UTF-8"?>
4+
<root>
5+
<citys>
6+
<!--
7+
城市信息
8+
-->
9+
{
10+
"1" : "上海",
11+
"2" : "北京",
12+
"3" : "成都"
13+
}
14+
</citys>
15+
</root>
16+
----
17+
同0017,XML里面嵌套JSON,觉累不爱。

JiYouMCC/0019/0019.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding: utf-8 -*-
2+
import xlrd
3+
from xml.dom.minidom import Document
4+
from xml.etree.ElementTree import Comment, Element
5+
import json
6+
7+
infos = []
8+
info_file = xlrd.open_workbook('numbers.xls')
9+
info_table = info_file.sheets()[0]
10+
row_count = info_table.nrows
11+
doc = Document()
12+
root = doc.createElement('root')
13+
doc.appendChild(root)
14+
numbers = doc.createElement('numbers')
15+
for row in range(row_count):
16+
number = doc.createElement('number')
17+
col1 = doc.createElement('column')
18+
col1.appendChild(doc.createTextNode('%d' % info_table.cell(row, 0).value))
19+
number.appendChild(col1)
20+
col2 = doc.createElement('column')
21+
col2.appendChild(doc.createTextNode('%d' % info_table.cell(row, 1).value))
22+
number.appendChild(col2)
23+
col3 = doc.createElement('column')
24+
col3.appendChild(doc.createTextNode('%d' % info_table.cell(row, 2).value))
25+
number.appendChild(col3)
26+
numbers.appendChild(number)
27+
root.appendChild(numbers)
28+
file = open('numbers.xml', 'w')
29+
file.write(doc.toprettyxml(indent=''))
30+
file.close()

JiYouMCC/0019/numbers.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" ?>
2+
<root>
3+
<numbers>
4+
<number>
5+
<column>1</column>
6+
<column>82</column>
7+
<column>65535</column>
8+
</number>
9+
<number>
10+
<column>20</column>
11+
<column>90</column>
12+
<column>13</column>
13+
</number>
14+
<number>
15+
<column>26</column>
16+
<column>809</column>
17+
<column>1024</column>
18+
</number>
19+
</numbers>
20+
</root>

JiYouMCC/0019/readme.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
**第 0019 题:** 将 第 0016 题中的 numbers.xls 文件中的内容写到 numbers.xml 文件中,如下
2+
3+
所示:
4+
5+
<?xml version="1.0" encoding="UTF-8"?>
6+
<root>
7+
<numbers>
8+
<!--
9+
数字信息
10+
-->
11+
12+
[
13+
[1, 82, 65535],
14+
[20, 90, 13],
15+
[26, 809, 1024]
16+
]
17+
18+
</numbers>
19+
</root>
20+
----
21+
同0017和0018,XML里面嵌套JSON,觉累不爱。

0 commit comments

Comments
 (0)