您的位置:首页 > 编程语言 > Python开发

Show me the code之Python练习册 Q17~19 xml操作

2016-04-18 09:26 615 查看
__file__ = 'A17.py'
__author__ = 'Jerry Liu'
__date__ = '2016-04-15'

"""
问题: 第 0014 题中的 student.xls 文件中的内容写到 student.xml 文件中,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<students>
<!--
学生信息表
"id" : [名字, 数学, 语文, 英文]
-->
{
"1" : ["张三", 150, 120, 100],
"2" : ["李四", 90, 99, 95],
"3" : ["王五", 60, 66, 68]
}
</students>
</root>
"""

from openpyxl.reader.excel import load_workbook
from xml.dom.minidom import Document

def getdata(path):
wb = load_workbook(path)
# 读取第一个sheet
snames = wb.get_sheet_names()
sheet = wb.get_sheet_by_name(snames[0])
# 创建xml
doc = Document()
# 创建根元素
root = doc.createElement('root')
doc.appendChild(root)
# 创建学生列表元素
stus = doc.createElement('students')
root.appendChild(stus)
for line in range(sheet.get_highest_row()):
line += 1
# 创建学生元素
stu = doc.createElement('student')
stus.appendChild(stu)
# 创建学生姓名元素
name = sheet.cell(row=line, column=1).value
stu_name = doc.createElement('Name')
stu_name.appendChild(doc.createTextNode(name))
stu.appendChild(stu_name)
# 创建英文成绩
yy_value = sheet.cell(row=line, column=2).value
yy_name = doc.createElement('English')
yy_name.appendChild(doc.createTextNode(str(yy_value)))
stu.appendChild(yy_name)
# 创建语文成绩
yw_value = sheet.cell(row=line, column=3).value
yw_name = doc.createElement('Chinese')
yw_name.appendChild(doc.createTextNode(str(yw_value)))
stu.appendChild(yw_name)
# 数学语文成绩
sx_value = sheet.cell(row=line, column=4).value
sx_name = doc.createElement('Math')
sx_name.appendChild(doc.createTextNode(str(sx_value)))
stu.appendChild(sx_name)

# 保存文件
f = open("d://student.xml", "w")
doc.writexml(f)
f.close()

if __name__ == '__main__':
getdata('d://student.xlsx')


__file__ = 'A18.py'
__author__ = 'Jerry Liu'
__date__ = '2016-04-15'

"""
问题:将 第 0015 题中的 city.xls 文件中的内容写到 city.xml 文件中,如下所示:
<?xmlversion="1.0" encoding="UTF-8"?>
<root>
<citys>
<!--
城市信息
-->
{
"1" : "上海",
"2" : "北京",
"3" : "成都"
}
</citys>
</root>
"""

from openpyxl.reader.excel import load_workbook
from xml.dom.minidom import Document

def getdata(path):
wb = load_workbook(path)
# 读取第一个sheet
snames = wb.get_sheet_names()
sheet = wb.get_sheet_by_name(snames[0])

cxml = Document()
eRoot =cxml.createElement('root')
cxml.appendChild(eRoot)
eCities = cxml.createElement('cities')
eRoot.appendChild(eCities)
eComments = cxml.createComment('城市信息')
eCities.appendChild(eComments)

# 定义字典
dict = {}
for line in range(sheet.get_highest_row()):
line += 1
id = sheet.cell(row=line, column=1).value
city = sheet.cell(row=line, column=2).value
dict[id] = city

eCities.appendChild(cxml.createTextNode(str(dict)))

f = open('d://city.xml', 'w')
cxml.writexml(f)
f.close()

if __name__ == '__main__':
getdata('d://city.xlsx')


__file__ = 'A19.py'
__author__ = 'Jerry Liu'
__date__ = '2016-04-18'

"""
问题:将 第 0016 题中的 numbers.xls 文件中的内容写到 numbers.xml 文件中,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<numbers>
<!--
数字信息
-->

[
[1, 82, 65535],
[20, 90, 13],
[26, 809, 1024]
]

</numbers>
</root>
"""
from openpyxl.reader.excel import load_workbook
from xml.dom.minidom import Document

def getdata(path):
wb = load_workbook(path)
# 读取第一个sheet
snames = wb.get_sheet_names()
sheet = wb.get_sheet_by_name(snames[0])

mdata = []
#读取数据
for line in range(sheet.get_highest_row()):
line += 1
list = []
list.append(sheet.cell(row=line, column=1).value)
list.append(sheet.cell(row=line, column=2).value)
list.append(sheet.cell(row=line, column=3).value)
mdata.append(list)

cxml = Document()
eRoot =cxml.createElement('root')
cxml.appendChild(eRoot)
eNums = cxml.createElement('numbers')
eRoot.appendChild(eNums)
eComments = cxml.createComment('数字信息')
eNums.appendChild(eComments)
eData = cxml.createTextNode(str(mdata))
eNums.appendChild(eData)

f = open('d://numbers.xml', 'w')
cxml.writexml(f)
f.close()

if __name__ == '__main__':
getdata('d://numbers.xlsx')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: