forked from Yixiaohan/show-me-the-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
the_0017th_problem.py
59 lines (48 loc) · 1.64 KB
/
the_0017th_problem.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding:utf-8 -*-
'''
果然在windows下就会遇见一大堆的编码问题,真是累。
话说我会用vim的其实……主要是VB太卡不想用。
'''
import json
import xml.dom.minidom as minidom
from collections import OrderedDict
import xlrd
import html.parser
class xls_to_xml():
def __init__(self, path):
self.path = path
self.xmlpath = path.replace("xls", "xml")
self.comment = '''
<!--
学生信息表
"id" : [名字, 数学, 语文, 英文]
-->
'''
#读取xls
def get_xls(self):
excel = xlrd.open_workbook(self.path)
sheet = excel.sheet_by_name("Sheet1")
dictory = OrderedDict()
for i in range(sheet.nrows):
values = sheet.row_values(i)
dictory[values[0]] = values[1:]
return dictory
#生成xml
def generate_xml(self):
dom = minidom.getDOMImplementation().createDocument(None, 'root', None)
root = dom.documentElement
students = dom.createElement("students")
root.appendChild(students)
comment = dom.createTextNode(self.comment)
dictory = json.dumps(self.get_xls(), ensure_ascii=False)
information = dom.createTextNode(dictory)
students.appendChild(comment)
students.appendChild(information)
with open(self.xmlpath, 'w') as file:
html_parser = html.parser.HTMLParser()
tranform = html_parser.unescape(dom.toxml())
file.write(tranform)
if __name__ == "__main__":
path = r"C:\Users\asus\Desktop\student.xls"
test = xls_to_xml(path)
test.generate_xml()