forked from Show-Me-the-Code/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0017.py
More file actions
34 lines (22 loc) · 1.11 KB
/
0017.py
File metadata and controls
34 lines (22 loc) · 1.11 KB
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
import xlrd
import xml.dom.minidom as md
def get_xls_data(filename):
book = xlrd.open_workbook(filename)
sheet = book.sheet_by_index(0)
content = {}
for i in range(sheet.nrows):
content[i+1] = sheet.row_values(i)[1:]
return content
def write_to_xml(xlscontent):
xmlfile = md.Document() #创建新xml文件
root = xmlfile.createElement('root') #创建节点
students = xmlfile.createElement('students') #创建节点
xmlfile.appendChild(root) #在文件中添加root节点
root.appendChild(students) #在root下添加students节点
comment = xmlfile.createComment('学生信息表 "id" : [名字, 数学, 语文, 英文]') #创建评论
students.appendChild(comment) #在students标签下添加comment
xmlcontent = xmlfile.createTextNode(str(xlscontent)) #创建文本节点
students.appendChild(xmlcontent) 在students标签下添加文本内容
with open('students.xml', 'wb') as f:
f.write(xmlfile.toprettyxml(encoding = 'utf-8')) #写入文件
write_to_xml(get_xls_data('students.xls'))