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
1,176 changes: 1,176 additions & 0 deletions burness/0014/readme.html

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions burness/0014/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## txt2xls.py


- 读入txt内容,为保证其结构,和方便存储,使用json保存:json.load()。以后做读入文本时**尽量使用结构化的库,这样方便操作**。
- 利用xlwt3库,新建workbook对象:xls_workbook = xlwt3.Workbook(),Workbook中添加sheet:xls_wordbook.add_sheet('sheet_name');然后对表进行操作,写入数据:write()。首先是写入列数,即1,2,3;然后对应列写入对应的数据。
- 保存后,乱码解决方法:1,字体问题,网上有贴表示可能是字体问题,发现使用Simsun字体后,还是会存在乱码,不是字体产生的。2,后来发现,不是程序的问题,是之前,我为了偷懒,直接拷贝的github上面的student.txt的内容,可能直接造成了编码的错误,之后自己在txt文件中写后,程序运行不存在乱码错误。

5 changes: 5 additions & 0 deletions burness/0014/student.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"1":["����",150,120,100],
"2":["����",90,99,95],
"3":["����",60,66,68]
}
Binary file added burness/0014/student.xls
Binary file not shown.
29 changes: 29 additions & 0 deletions burness/0014/txt2xls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#-*- coding: utf-8-*-
# xlwt3的参考文档见http://pythonhosted.org//xlwt3/

import xlwt3
import json


def txt_to_xls(txt_file):
with open(txt_file,'r') as f:
file_content = json.load(f,encoding = 'utf-8')
xls_workbook = xlwt3.Workbook(encoding = 'utf-8')
xls_sheet = xls_workbook.add_sheet('student')
style = xlwt3.XFStyle()
font = xlwt3.Font()
font.name = '黑体'
style.font = font
for i in range(len(file_content)):
# 写入对应列号
xls_sheet.write(i, 0, i+1,style)
json_data = file_content[str(i+1)]
for j in range(len(json_data)):
xls_sheet.write(i,j+1,json_data[j],style)
xls_workbook.save('student.xls')




if __name__ == '__main__':
txt_to_xls('student.txt')
5 changes: 5 additions & 0 deletions burness/0015/city.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"1":"����",
"2":"�Ϻ�",
"3":"�ɶ�"
}
Binary file added burness/0015/city.xls
Binary file not shown.
29 changes: 29 additions & 0 deletions burness/0015/txt2xls2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#-*- coding: utf-8-*-
import xlwt3
import json


def txt_to_xls(txt_file):
with open(txt_file,'r') as f:
# print(f)
file_content = json.load(f)
# print(file_content)
xls_workbook = xlwt3.Workbook()
xls_sheet = xls_workbook.add_sheet('city')
style = xlwt3.XFStyle()
font = xlwt3.Font()
font.name = '黑体'
style.font = font
for i in range(len(file_content)):
# 写入对应列号
xls_sheet.write(i, 0, i+1,style)
json_data = file_content[str(i+1)]
print(json_data)
xls_sheet.write(i,1,json_data)
xls_workbook.save('city.xls')




if __name__ == '__main__':
txt_to_xls('city.txt')
5 changes: 5 additions & 0 deletions burness/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 burness/0016/numbers.xls
Binary file not shown.
24 changes: 24 additions & 0 deletions burness/0016/txt2xls3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#-*- coding: utf-8-*-
import xlwt3
import json


def txt_to_xls(txt_file):
with open(txt_file,'r') as f:
# print(f)
file_content = json.load(f)
print(file_content)
print(file_content[0])
xls_workbook = xlwt3.Workbook()
xls_sheet = xls_workbook.add_sheet('numbers')
style = xlwt3.XFStyle()
font = xlwt3.Font()
font.name = '黑体'
style.font = font
for i in range(len(file_content)):
for j in range(len(file_content[i])):
xls_sheet.write(i,j,file_content[i][j])
xls_workbook.save('numbers.xls')

if __name__ == '__main__':
txt_to_xls('numbers.txt')
22 changes: 22 additions & 0 deletions burness/0017/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## xls2xml.py (读入xls内容,转化为xml保存)

1. xls数据读入问题:读入xls文件,excel = xlrd.open_workbook(file_name);选定对应sheet,table = excel.sheet_by_name('student');按行读入指定表内容for i in range(nrows): print table.row_values(i)。
2. 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>

其中root:root = etree.Element("root");students:students = etree.SubElement(root, "students");comment:student.append(etree.Comment('some comment'));text:students.text = "TEXT"即为xls中读取的内容.
3. 保存问题:output = codecs.open('students.xml','w','utf-8');students_xml = etree.ElementTree(root);output.write(etree.tounicode(students_xml.getroot()));output.close()
Binary file added burness/0017/student.xls
Binary file not shown.
2 changes: 2 additions & 0 deletions burness/0017/students.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<root><students>{'3': "['王五', 60.0, 66.0, 68.0]", '2': "['李四', 90.0, 99.0, 95.0]", '1': "['张三', 150.0, 120.0, 100.0]"}<!--学生信息表
"id": [名字,数学,语文,英语]--></students></root>
31 changes: 31 additions & 0 deletions burness/0017/xls2xml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#-*- coding: utf-8-*-
import xlrd,codecs
from lxml import etree

def xls_xml(file_name):
data = {}

excel = xlrd.open_workbook(file_name)
table = excel.sheet_by_name('student')
# print(table.row_values(0))
nrows = table.nrows
for i in range(nrows):
key = str(int(table.row_values(i)[0]))
value = str(table.row_values(i)[1:])
data[key] = value

output = codecs.open('students.xml','w','utf-8')
root = etree.Element('root')
students_xml = etree.ElementTree(root)
students = etree.SubElement(root, 'students')
students.append(etree.Comment('学生信息表\n\"id\": [名字,数学,语文,英语]'))
students.text = str(data)
output.write(etree.tounicode(students_xml.getroot()))
output.close()




if __name__ == '__main__':
xls_xml('student.xls')

Binary file added burness/0018/city.xls
Binary file not shown.
1 change: 1 addition & 0 deletions burness/0018/citys.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<root><citys>{'2': "['上海']", '3': "['成都']", '1': "['北京']"}<!--城市信息--></citys></root>
31 changes: 31 additions & 0 deletions burness/0018/xls2xml2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#-*- coding: utf-8-*-
import xlrd,codecs
from lxml import etree

def xls_xml(file_name):
data = {}

excel = xlrd.open_workbook(file_name)
table = excel.sheet_by_name('city')
# print(table.row_values(0))
nrows = table.nrows
for i in range(nrows):
key = str(int(table.row_values(i)[0]))
value = str(table.row_values(i)[1:])
data[key] = value

output = codecs.open('citys.xml','w','utf-8')
root = etree.Element('root')
citys_xml = etree.ElementTree(root)
citys = etree.SubElement(root, 'citys')
citys.append(etree.Comment('城市信息'))
citys.text = str(data)
output.write(etree.tounicode(citys_xml.getroot()))
output.close()




if __name__ == '__main__':
xls_xml('city.xls')

Binary file added burness/0019/numbers.xls
Binary file not shown.
1 change: 1 addition & 0 deletions burness/0019/numbers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<root><numbers>[[1.0, 82.0, 65535.0], [20.0, 90.0, 13.0], [26.0, 809.0, 1024.0]]<!--数字信息--></numbers></root>
28 changes: 28 additions & 0 deletions burness/0019/xls2xml3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#-*- coding: utf-8-*-
import xlrd,codecs
from lxml import etree

def xls_xml(file_name):
data = []

excel = xlrd.open_workbook(file_name)
table = excel.sheet_by_name('numbers')
# print(table.row_values(0))
nrows = table.nrows
for i in range(nrows):
data.append(table.row_values(i))
output = codecs.open('numbers.xml','w','utf-8')
root = etree.Element('root')
numbers_xml = etree.ElementTree(root)
numbers = etree.SubElement(root, 'numbers')
numbers.append(etree.Comment('数字信息'))
numbers.text = str(data)
output.write(etree.tounicode(numbers_xml.getroot()))
output.close()




if __name__ == '__main__':
xls_xml('numbers.xls')