Skip to content

Commit 51d85cf

Browse files
committed
Fix 0017
1 parent a334b1b commit 51d85cf

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

Drake-Z/0017/0017.py

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,40 @@
2020

2121
__author__ = 'Drake-Z'
2222

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')
23+
import xlrd,codecs
24+
from lxml import etree
25+
from collections import OrderedDict
26+
27+
def read_xls(filename):
28+
data = xlrd.open_workbook(filename)
3729
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)
30+
c = OrderedDict()
31+
for i in range(table.nrows):
32+
c[table.cell(i,0).value] = table.row_values(i)[1:]
33+
return c
34+
35+
def save_xml(data):
36+
output = codecs.open('student.xml','w','utf-8')
37+
root = etree.Element('root')
38+
student_xml = etree.ElementTree(root)
39+
student = etree.SubElement(root, 'student')
40+
student.append(etree.Comment('学生信息表\n\"id\": [名字,数学,语文,英语]'))
41+
student.text = str(data)
42+
output.write(etree.tounicode(student_xml.getroot()))
43+
output.close()
44+
45+
if __name__ == '__main__':
46+
file = 'student.xls'
47+
a = read_xls(file)
48+
save_xml(a)
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+

0 commit comments

Comments
 (0)