Skip to content

Commit 0837ba0

Browse files
committed
Merge pull request Show-Me-the-Code#150 from Jaccorot/master
add 0010-0016
2 parents 64db1e4 + 9bff757 commit 0837ba0

13 files changed

Lines changed: 247 additions & 0 deletions

File tree

Jaccorot/0009/0009.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#!/usr/bin/python
22
#coding=utf-8
33

4+
"""
5+
6+
第 0009 题:一个HTML文件,找出里面的链接
7+
8+
"""
9+
410
from bs4 import BeautifulSoup
511

612
def find_the_link(filepath):

Jaccorot/0010/0010.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#!/usr/bin/python
22
#coding=utf-8
33

4+
"""
5+
6+
第 0010 题:使用 Python 生成字母验证码图片
7+
8+
"""
9+
410
from PIL import Image, ImageDraw, ImageFont, ImageFilter
511
import random
612

Jaccorot/0011/0011.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/python
2+
# coding=utf-8
3+
4+
"""
5+
第 0011 题: 敏感词文本文件 filtered_words.txt,里面的内容为以下内容,当用户输入敏感词语时,则打印出 Freedom,
6+
否则打印出 Human Rights。
7+
"""
8+
9+
10+
def trans_to_words():
11+
type_in = raw_input(">")
12+
judge_flag = False
13+
with open('filtered_words.txt') as f:
14+
text = f.read().decode('utf-8').encode('gbk')
15+
16+
for i in text.split("\n"):
17+
if i in type_in:
18+
judge_flag = True
19+
20+
if judge_flag:
21+
print "Freedom"
22+
else:
23+
print "Human Rights"
24+
25+
26+
if __name__ == "__main__":
27+
while True:
28+
trans_to_words()

Jaccorot/0011/filtered_words.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
北京
2+
程序员
3+
公务员
4+
领导
5+
牛比
6+
牛逼
7+
你娘
8+
你妈
9+
love
10+
sex
11+
jiangge

Jaccorot/0012/0012.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/python
2+
# coding=utf-8
3+
4+
"""
5+
第 0012 题: 敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,
6+
当用户输入敏感词语,则用 星号 * 替换,例如当用户输入「北京是个好城市」,则变成「**是个好城市」。
7+
"""
8+
9+
10+
def trans_to_words():
11+
type_in = raw_input(">")
12+
with open('filtered_words.txt') as f:
13+
text = f.read().decode('utf-8').encode('gbk')
14+
print text.split("\n")
15+
for i in text.split("\n"):
16+
if i in type_in:
17+
type_in = type_in.replace(i, '**')
18+
print type_in
19+
20+
if __name__ == "__main__":
21+
while True:
22+
trans_to_words()

Jaccorot/0012/filtered_words.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
北京
2+
程序员
3+
公务员
4+
领导
5+
牛比
6+
牛逼
7+
你娘
8+
你妈
9+
love
10+
sex
11+
jiangge

Jaccorot/0013/0013.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/python
2+
# coding=utf-8
3+
4+
"""
5+
第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-)
6+
"""
7+
8+
import os
9+
import urllib
10+
from bs4 import BeautifulSoup
11+
from urlparse import urlsplit
12+
13+
14+
def catch_tieba_pics(url):
15+
content = urllib.urlopen(url)
16+
bs = BeautifulSoup(content, 'lxml')
17+
for i in bs.find_all('img', {"class": "BDE_Image"}):
18+
download_pic(i['src'])
19+
20+
21+
def download_pic(url):
22+
image_content = urllib.urlopen(url).read()
23+
file_name = os.path.basename(urlsplit(url)[2])
24+
output = open(file_name, 'wb')
25+
output.write(image_content)
26+
output.close()
27+
28+
29+
if __name__ == '__main__':
30+
catch_tieba_pics('http://tieba.baidu.com/p/2166231880')

Jaccorot/0014/0014.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/python
2+
# coding=utf-8
3+
4+
"""
5+
第 0014 题: 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示,
6+
请将上述内容写到 student.xls 文件中,如下图所示:
7+
"""
8+
9+
import os
10+
import json
11+
import xlwt
12+
13+
def read_txt(path):
14+
with open(path, 'r') as f:
15+
text = f.read().decode('utf-8')
16+
text_json = json.loads(text)
17+
return text_json
18+
19+
20+
def save_into_excel(content_dict, excel_name):
21+
wb = xlwt.Workbook()
22+
ws = wb.add_sheet("student", cell_overwrite_ok=True)
23+
row = 0
24+
col = 0
25+
26+
for k, v in sorted(content_dict.items(),key=lambda d:d[0]):
27+
ws.write(row, col, k)
28+
for i in v:
29+
col += 1
30+
ws.write(row, col, i)
31+
32+
row += 1
33+
col = 0
34+
35+
wb.save(excel_name)
36+
37+
38+
if __name__ == "__main__":
39+
read_content = read_txt(os.path.join(os.path.split(__file__)[0], 'student.txt'))
40+
save_into_excel(read_content, 'student.xls')
41+
42+

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

Jaccorot/0015/0015.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/python
2+
# coding=utf-8
3+
4+
"""
5+
纯文本文件 city.txt为城市信息, 里面的内容(包括花括号)如下所示:
6+
请将上述内容写到 city.xls 文件中,如下图所示:
7+
"""
8+
9+
import os
10+
import json
11+
import xlwt
12+
13+
def read_txt(path):
14+
with open(path, 'r') as f:
15+
text = f.read().decode('utf-8')
16+
text_json = json.loads(text)
17+
return text_json
18+
19+
20+
def save_into_excel(content_dict, excel_name):
21+
wb = xlwt.Workbook()
22+
ws = wb.add_sheet("city", cell_overwrite_ok=True)
23+
row = 0
24+
col = 0
25+
26+
for k, v in sorted(content_dict.items(),key=lambda d:d[0]):
27+
ws.write(row, col, k)
28+
col += 1
29+
ws.write(row, col, v)
30+
31+
row += 1
32+
col = 0
33+
34+
wb.save(excel_name)
35+
36+
37+
if __name__ == "__main__":
38+
read_content = read_txt(os.path.join(os.path.split(__file__)[0], 'city.txt'))
39+
save_into_excel(read_content, 'city.xls')

0 commit comments

Comments
 (0)