Skip to content

Commit 2e3ab19

Browse files
authored
Merge pull request Show-Me-the-Code#246 from monkey-soft/master
add 0009 0014 0015 0016
2 parents b5466d4 + bb7ec85 commit 2e3ab19

13 files changed

+1750
-0
lines changed

monkey/0008/The world's leading software development platform · GitHub.html

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

monkey/0008/main.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding:utf-8 -*-
2+
3+
'''
4+
5+
第 0008 题:一个HTML文件,找出里面的正文。
6+
7+
@Author monkey
8+
@Date 2017-8-31
9+
'''
10+
import json
11+
12+
from bs4 import BeautifulSoup
13+
14+
15+
def findContent():
16+
path = "The world's leading software development platform · GitHub.html"
17+
18+
with open(path, encoding='UTF-8') as file:
19+
soup = BeautifulSoup(file)
20+
21+
# print(soup.prettify())
22+
print(soup.body)
23+
24+
25+
26+
if __name__ == '__main__':
27+
findContent()

monkey/0009/The world's leading software development platform · GitHub.html

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

monkey/0009/main.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding:utf-8 -*-
2+
3+
'''
4+
5+
第 0009 题:一个HTML文件,找出里面的链接。
6+
7+
@Author monkey
8+
@Date 2017-8-31
9+
'''
10+
import json
11+
12+
from bs4 import BeautifulSoup
13+
14+
15+
def findTagA():
16+
path = "The world's leading software development platform · GitHub.html"
17+
18+
with open(path, encoding='UTF-8') as file:
19+
soup = BeautifulSoup(file)
20+
21+
# print(soup.prettify())
22+
links = []
23+
for i in soup.find_all('a'):
24+
links.append(i['href'])
25+
26+
print(links)
27+
28+
if __name__ == '__main__':
29+
findTagA()

monkey/0014/main.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -*- coding:utf-8 -*-
2+
3+
'''
4+
5+
第 0014 题: 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示:
6+
{
7+
"1":["张三",150,120,100],
8+
"2":["李四",90,99,95],
9+
"3":["王五",60,66,68]
10+
}
11+
请将上述内容写到 student.xls 文件中。
12+
13+
@Author monkey
14+
@Date 2017-8-31
15+
'''
16+
import json
17+
import xlwt
18+
19+
def getStudent():
20+
21+
with open('student.txt', 'r', encoding = 'UTF-8') as file:
22+
text = ''
23+
for line in file:
24+
text = text + line
25+
26+
stu_json = json.loads(text, encoding = 'UTF-8')
27+
28+
print(stu_json)
29+
30+
writeInXLS(stu_json)
31+
32+
33+
def writeInXLS(dict):
34+
fileName = 'student.xls'
35+
# 创建 xls 文件
36+
file = xlwt.Workbook(encoding = 'utf-8')
37+
# 创建 表
38+
sheet = file.add_sheet('student', cell_overwrite_ok=True)
39+
40+
row = 0
41+
col = 0
42+
43+
for k, v in sorted(dict.items(), key=lambda d:d[0]):
44+
sheet.write(row, col, k)
45+
for i in v:
46+
col += 1
47+
sheet.write(row, col, i)
48+
49+
row += 1
50+
col = 0
51+
52+
file.save(fileName)
53+
print('写入成功')
54+
55+
if __name__ == '__main__':
56+
getStudent()

monkey/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+
}

monkey/0014/student.xls

5.5 KB
Binary file not shown.

monkey/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+
}

monkey/0015/city.xls

5.5 KB
Binary file not shown.

monkey/0015/main.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# -*- coding:utf-8 -*-
2+
3+
'''
4+
第 0015 题: 纯文本文件 city.txt为城市信息, 里面的内容(包括花括号)如下所示:
5+
6+
{
7+
"1" : "上海",
8+
"2" : "北京",
9+
"3" : "成都"
10+
}
11+
请将上述内容写到 city.xls 文件中,如下图所示:
12+
13+
@Author monkey
14+
@Date 2017-8-31
15+
'''
16+
import json
17+
import xlwt
18+
19+
def getCity():
20+
21+
with open('city.txt', 'r', encoding='UTF-8') as file:
22+
text = ''
23+
for line in file:
24+
text = text + line
25+
26+
city_json = json.loads(text, encoding = 'UTF-8')
27+
print(city_json)
28+
29+
writeInXLS(city_json)
30+
31+
32+
def writeInXLS(dict):
33+
fileName = 'city.xls'
34+
35+
# 创建 文件
36+
file = xlwt.Workbook()
37+
# 创建 表
38+
sheet = file.add_sheet('city', cell_overwrite_ok=True)
39+
40+
row = 0
41+
col = 0
42+
43+
for k, v in sorted(dict.items(), key=lambda d:d[0]):
44+
sheet.write(row, col, k)
45+
col += 1
46+
sheet.write(row, col, v)
47+
48+
row += 1
49+
col = 0
50+
51+
file.save(fileName)
52+
53+
if __name__ == '__main__':
54+
getCity()

0 commit comments

Comments
 (0)