Skip to content

Commit cf04b08

Browse files
author
lwhhhh
committed
14题
1 parent 407fae2 commit cf04b08

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

lwh/14/SaveToExcel.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from openpyxl import Workbook
2+
from openpyxl import load_workbook
3+
4+
path_save = "/home/lwh/SublimeTextProject/test.xlsx"
5+
path_read = "/home/lwh/SublimeTextProject/demo.txt"
6+
7+
8+
def write_test():
9+
wb = Workbook() # 1.creat a work book
10+
wb_sheet0 = wb.create_sheet("student") # 2.create a sheet in the work book
11+
wb_sheet0 = wb.active
12+
13+
datas_str = ""
14+
datas_dict = {}
15+
16+
with open(path_read, "r") as f:
17+
lines = f.readlines()
18+
19+
# process the data from txt file.I make it be a dict
20+
for line in lines:
21+
if len(line.strip()) > 1:
22+
key, value = line.split(':')
23+
key = key.strip().strip("\"")
24+
value = value.strip().strip(",").strip("[").strip("]")
25+
value_list = value.split(",")
26+
# print(len(value_list))
27+
datas_dict[key] = value_list
28+
29+
for i in range(1, 4): # 3.assign value to cell of the sheet
30+
for j in range(1, 5):
31+
cell = wb_sheet0.cell(row=i, column=j + 1)
32+
cell.value = datas_dict[str(i)][j - 1]
33+
# print(cell.value)
34+
cell = wb_sheet0.cell(row=i, column=1)
35+
cell.value = i
36+
'''
37+
for row in wb_sheet0.iter_rows():
38+
for cell in row:
39+
print(cell.value)
40+
'''
41+
wb.save(path_save) # 4.save the file
42+
print('success')
43+
44+
write_test()

lwh/14/demo.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+
}

lwh/14/demo.txt~

Whitespace-only changes.

0 commit comments

Comments
 (0)