pycode0018

第 0018 题

将 第 0015 题中的 city.xls 文件中的内容写到 city.xml 文件中,如下所示:

<?xmlversion="1.0" encoding="UTF-8"?>
<root>
<cities>
<!--
    城市信息
-->
{
    "1" : "上海",
    "2" : "北京",
    "3" : "成都"
}
</cities>
</root>

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
from openpyxl import load_workbook
from case0017 import dumpxml
def (path, wbname):
"""
载入xlsx文件内容
:param path: 文件路径
:param wbname: wb名称
:return: 返回文件内容(dict)
"""
wb = load_workbook(filename=path)
ws = wb[wbname]
content = {}
for i in range(0, 3):
content[ws.cell(row=i + 1, column = 1).value] = ws.cell(row=i + 1, column = 2).value
return content
if __name__ == '__main__':
path = 'resource/city.xlsx'
wbname = 'city'
content = loadxlsx(path, wbname)
comment = 'nt城市信息nt'
element = 'cities'
output = 'resource/city.xml'
dumpxml(content, comment, element, output)