Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added renzongxian/0010/t1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added renzongxian/0014/student.xls
Binary file not shown.
Binary file added renzongxian/0015/city.xls
Binary file not shown.
39 changes: 39 additions & 0 deletions renzongxian/0016/0016.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Source:https://github.com/Show-Me-the-Code/show-me-the-code
# Author:renzongxian
# Date:2014-12-22
# Python 3.4

"""

第 0016 题: 纯文本文件 numbers.txt, 里面的内容(包括方括号)如下所示:
[
[1, 82, 65535],
[20, 90, 13],
[26, 809, 1024]
]
请将上述内容写到 numbers.xls 文件中.

"""

import xlwt3
import json


def write_txt_to_xls(txt_file):
# Read form the txt file
txt_object = open(txt_file, 'r')
file_content = json.load(txt_object)

# Write to the xls file
xls_object = xlwt3.Workbook()
sheet = xls_object.add_sheet('numbers')
for i in range(len(file_content)):
data = file_content[i]
for j in range(len(data)):
sheet.write(i, j, data[j])
xls_object.save('numbers.xls')


if __name__ == '__main__':
write_txt_to_xls('numbers.txt')

5 changes: 5 additions & 0 deletions renzongxian/0016/numbers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
[1, 82, 65535],
[20, 90, 13],
[26, 809, 1024]
]
Binary file added renzongxian/0016/numbers.xls
Binary file not shown.
66 changes: 66 additions & 0 deletions renzongxian/0017/0017.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Source:https://github.com/Show-Me-the-Code/show-me-the-code
# Author:renzongxian
# Date:2014-12-22
# Python 3.4

"""

第 0017 题: 将 第 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>

"""

import xlrd
from xml.dom import minidom, Node


def open_xls():
excel = xlrd.open_workbook("student.xls")
student_sheet = excel.sheet_by_name("student")
sheet_content = {}
for row in range(student_sheet.nrows):
row_value = student_sheet.row_values(row)
for i in range(len(row_value)):
if type(row_value[i]) == float:
row_value[i] = int(row_value[i])
sheet_content[str(row+1)] = row_value[1:]
return sheet_content


def build_xml(content):
# Create Dom Object
doc = minidom.Document()
# Create root tag
root = doc.createElement('root')
doc.appendChild(root)
# Create 'students' tag
students = doc.createElement('students')
root.appendChild(students)
# Create comment element
students.appendChild(doc.createComment("学生信息表\"id\" : [名字, 数学, 语文, 英文]"))
# Create text element
students.appendChild(doc.createTextNode(str(content)))

# Save the xml file
student_xml = open('student.xml', 'w')
student_xml.write(doc.toprettyxml())
student_xml.close()

if __name__ == '__main__':
_content = open_xls()
build_xml(_content)
Binary file added renzongxian/0017/student.xls
Binary file not shown.
7 changes: 7 additions & 0 deletions renzongxian/0017/student.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" ?>
<root>
<students>
<!--ѧ����Ϣ��"id" : [����, ��ѧ, ����, Ӣ��]-->
{'3': ['����', 60, 66, 68], '1': ['����', 150, 120, 100], '2': ['����', 90, 99, 95]}
</students>
</root>