Skip to content

Commit a27e38d

Browse files
committed
Create 0017.py
1 parent 2f21a62 commit a27e38d

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Liez-python-code/0017/0017.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import xlrd
2+
import xml.dom.minidom as md
3+
4+
5+
def get_xls_data(filename):
6+
book = xlrd.open_workbook(filename)
7+
sheet = book.sheet_by_index(0)
8+
content = {}
9+
for i in range(sheet.nrows):
10+
content[i+1] = sheet.row_values(i)[1:]
11+
return content
12+
13+
14+
def write_to_xml(xlscontent):
15+
16+
xmlfile = md.Document()  #创建新xml文件
17+
18+
root = xmlfile.createElement('root')  #创建节点
19+
students = xmlfile.createElement('students')  #创建节点
20+
21+
xmlfile.appendChild(root)  #在文件中添加root节点
22+
root.appendChild(students)  #在root下添加students节点
23+
24+
comment = xmlfile.createComment('学生信息表 "id" : [名字, 数学, 语文, 英文]')  #创建评论
25+
students.appendChild(comment)  #在students标签下添加comment
26+
27+
xmlcontent = xmlfile.createTextNode(str(xlscontent))    #创建文本节点
28+
students.appendChild(xmlcontent)  在students标签下添加文本内容
29+
30+
with open('students.xml', 'wb') as f:
31+
f.write(xmlfile.toprettyxml(encoding = 'utf-8'))  #写入文件
32+
33+
34+
write_to_xml(get_xls_data('students.xls'))

0 commit comments

Comments
 (0)