Skip to content

Commit 88c6d2f

Browse files
committed
add 0014-0019
1 parent 8285cc1 commit 88c6d2f

21 files changed

Lines changed: 1396 additions & 0 deletions

burness/0014/readme.html

Lines changed: 1176 additions & 0 deletions
Large diffs are not rendered by default.

burness/0014/readme.md

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

burness/0014/student.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"1":["����",150,120,100],
3+
"2":["����",90,99,95],
4+
"3":["����",60,66,68]
5+
}

burness/0014/student.xls

5.5 KB
Binary file not shown.

burness/0014/txt2xls.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#-*- coding: utf-8-*-
2+
# xlwt3的参考文档见http://pythonhosted.org//xlwt3/
3+
4+
import xlwt3
5+
import json
6+
7+
8+
def txt_to_xls(txt_file):
9+
with open(txt_file,'r') as f:
10+
file_content = json.load(f,encoding = 'utf-8')
11+
xls_workbook = xlwt3.Workbook(encoding = 'utf-8')
12+
xls_sheet = xls_workbook.add_sheet('student')
13+
style = xlwt3.XFStyle()
14+
font = xlwt3.Font()
15+
font.name = '黑体'
16+
style.font = font
17+
for i in range(len(file_content)):
18+
# 写入对应列号
19+
xls_sheet.write(i, 0, i+1,style)
20+
json_data = file_content[str(i+1)]
21+
for j in range(len(json_data)):
22+
xls_sheet.write(i,j+1,json_data[j],style)
23+
xls_workbook.save('student.xls')
24+
25+
26+
27+
28+
if __name__ == '__main__':
29+
txt_to_xls('student.txt')

burness/0015/city.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"1":"����",
3+
"2":"�Ϻ�",
4+
"3":"�ɶ�"
5+
}

burness/0015/city.xls

5.5 KB
Binary file not shown.

burness/0015/txt2xls2.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#-*- coding: utf-8-*-
2+
import xlwt3
3+
import json
4+
5+
6+
def txt_to_xls(txt_file):
7+
with open(txt_file,'r') as f:
8+
# print(f)
9+
file_content = json.load(f)
10+
# print(file_content)
11+
xls_workbook = xlwt3.Workbook()
12+
xls_sheet = xls_workbook.add_sheet('city')
13+
style = xlwt3.XFStyle()
14+
font = xlwt3.Font()
15+
font.name = '黑体'
16+
style.font = font
17+
for i in range(len(file_content)):
18+
# 写入对应列号
19+
xls_sheet.write(i, 0, i+1,style)
20+
json_data = file_content[str(i+1)]
21+
print(json_data)
22+
xls_sheet.write(i,1,json_data)
23+
xls_workbook.save('city.xls')
24+
25+
26+
27+
28+
if __name__ == '__main__':
29+
txt_to_xls('city.txt')

burness/0016/numbers.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
[1,82,65535],
3+
[20,90,13],
4+
[26,809,1024]
5+
]

burness/0016/numbers.xls

5.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)