Skip to content

Commit d902166

Browse files
committed
Merge pull request Show-Me-the-Code#165 from yemaobumei/master
Python 练习册,每天一个小程序
2 parents 349a92a + 8f14088 commit d902166

File tree

18 files changed

+325
-1
lines changed

18 files changed

+325
-1
lines changed

python

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Subproject commit d989ede362d8938f44dbf7358ec935823a32e9cd
1+
Subproject commit 4ae3b1cc45266a9a769960696ead49087cc02cd3

yefan/001/001.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
分析
6+
其实要生成激活码(邀请码)也是很简单的事, 比如随机生成.或者使用GUID,UUID等,非常简单
7+
8+
但是我们得考虑存入以及验证的问题.
9+
10+
这里我参考产生唯一随机码的方法分析。这篇文章的思路:
11+
12+
主键+随机码的方式.
13+
14+
这种方法优点:使用也比较简单,不用直接去查询数据库,而最大的优点是查询的时候,可以根据邀请码直接得到主键id, 然后根据id去数据库查询(速度很快),再比较查询出来的邀请码和用户提交的邀请码是否一致。
15+
16+
生成:id(数据库primary key )->16进制 + "L(标识符)" +随机码
17+
获取id:获取16进制的id再转回10进制
18+
"""
19+
20+
21+
import random
22+
import string
23+
24+
def activation_code(id,length=10):
25+
'''
26+
id + L + 随机码
27+
string模块中的3个函数:string.letters,string.printable,string.printable
28+
'''
29+
prefix = hex(int(id))[2:]+ 'L'
30+
length = length - len(prefix)
31+
chars=string.ascii_letters+string.digits
32+
return prefix + ''.join([random.choice(chars) for i in range(length)])
33+
34+
def get_id(code):
35+
''' Hex to Dec '''
36+
return str(int(code.upper(), 16))
37+
38+
if __name__=="__main__":
39+
for i in range(10,500,35):
40+
code = activation_code(i)
41+
id_hex = code.split('L')[0]
42+
id = get_id(id_hex)
43+
print code,id

yefan/004/004.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
python实现任一个英文的纯文本文件,统计其中的单词出现的个数、行数、字符数
6+
"""
7+
8+
file_name = "movie.txt"
9+
10+
line_counts = 0
11+
word_counts = 0
12+
character_counts = 0
13+
14+
with open('C:\Python27\oneday_one\movie.txt', 'r') as f:
15+
for line in f:
16+
words = line.split()
17+
18+
line_counts += 1
19+
word_counts += len(words)
20+
character_counts += len(line)
21+
22+
print "line_counts ", line_counts
23+
print "word_counts ", word_counts
24+
print "character_counts ", character_counts

yefan/007/007.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding: utf-8 -*-
2+
3+
4+
#list all the files in your path(完整路径名path\**.py)
5+
import os
6+
def get_files(path):
7+
files=os.listdir(path)
8+
files_path=[]
9+
for fi in files:
10+
fi_path= path+'\\' + fi
11+
if os.path.isfile(fi_path):
12+
if fi.split('.')[-1]=='py':
13+
files_path.append(fi_path)
14+
elif(os.path.isdir(fi_path)):
15+
files_path+=get_files(fi_path)
16+
return files_path
17+
18+
# Count lines and blank lines and note lines in designated files
19+
def count_lines(files):
20+
line, blank, note = 0, 0, 0
21+
for filename in files:
22+
f = open(filename, 'rb')
23+
for l in f:
24+
l = l.strip()
25+
line += 1
26+
if l == '':
27+
blank += 1
28+
elif l[0] == '#' or l[0] == '/':
29+
note += 1
30+
f.close()
31+
return (line, blank, note)
32+
33+
if __name__ == '__main__':
34+
a=r'c:\python27'
35+
#files = get_files(r'c:\python27\oneday_one')
36+
files = get_files(r'F\v6:')
37+
print len(files),files
38+
lines = count_lines(files)
39+
print 'Line(s): %d, black line(s): %d, note line(s): %d' % (lines[0], lines[1], lines[2])
40+

yefan/008/008.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/python
2+
#coding=utf-8
3+
4+
"""
5+
第 0008 题:一个HTML文件,找出里面的正文。
6+
"""
7+
8+
from bs4 import BeautifulSoup
9+
10+
def find_the_content(path):
11+
with open(path) as f:
12+
text = BeautifulSoup(f, 'lxml')
13+
content = text.get_text().strip('\n')
14+
15+
return content.encode('gbk','ignore')
16+
17+
18+
if __name__ == '__main__':
19+
print find_the_content(r'D:\Show-Me-the-Code_show-me-the-code_1.html')

yefan/009/009.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/python
2+
#coding=utf-8
3+
4+
"""
5+
第 0009 题:一个HTML文件,找出里面的链接
6+
"""
7+
8+
from bs4 import BeautifulSoup
9+
10+
def find_the_link(filepath):
11+
links = []
12+
with open(filepath) as f:
13+
bs =BeautifulSoup(f,'lxml')
14+
for i in bs.find_all('a'):
15+
links.append(i['href'])
16+
return links
17+
18+
if __name__ == '__main__':
19+
#print find_the_link('D:\Show-Me-the-Code_show-me-the-code_1.html')
20+

yefan/011/011.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/env python
2+
# -*- coding: utf-8 -*-
3+
import codecs
4+
def read_txt():
5+
l=[]
6+
with codecs.open(r'c:\python27\oneday_one\1.txt') as fp:
7+
for line in fp.readlines():
8+
l.append(line.strip())
9+
return l
10+
11+
def check(l):
12+
word=raw_input('word:')
13+
for each_word in l:
14+
if word==each_word:
15+
print 'Freedom'
16+
return None
17+
print 'Human rights'
18+
return None
19+
20+
def main():
21+
l=read_txt()
22+
check(l)
23+
print l
24+
25+
if __name__=='__main__':
26+
main()
27+
28+

yefan/013/013.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+
第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-)
6+
"""
7+
8+
import os
9+
import urllib
10+
from bs4 import BeautifulSoup
11+
from urlparse import urlsplit
12+
import re
13+
14+
def catch_tieba_pics(url):
15+
content =urllib.urlopen(url)
16+
print type(content)
17+
#f.write(content.read())
18+
bs = BeautifulSoup(content, 'lxml')
19+
print type(bs)
20+
print bs.prettify() ################
21+
for i in bs.find_all('img', {"class": "BDE_Image"}):
22+
download_pic(i['src'])
23+
24+
def download_pic(url):
25+
image_content = urllib.urlopen(url).read()
26+
file_name = os.path.basename(urlsplit(url)[2])
27+
output = open(file_name, 'wb')
28+
output.write(image_content)
29+
output.close()
30+
31+
32+
if __name__ == '__main__':
33+
#catch_tieba_pics('http://tieba.baidu.com/p/2166231880')
34+
catch_tieba_pics('http://tieba.baidu.com/p/4203526008')
35+
#catch_tieba_pics('http://www.zhihu.com/question/22995735')
36+
37+
"""
38+
为什么知乎的网页print内容只有一点点
39+
"""

yefan/014/014.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# coding = utf-8
2+
__author__ = 'Forec'
3+
import xlwt
4+
import re
5+
6+
book = xlwt.Workbook(encoding = 'utf-8', style_compression=0)
7+
sheet = book.add_sheet('student',cell_overwrite_ok = True)
8+
line = 0
9+
info = re.compile(r'\"(\d+)\":\[\"(.*?)\",(\d+),(\d+),(\d+)\]')
10+
with open('student.txt',"r") as f:
11+
data = f.read()
12+
data=data.decode('gbk').encode('utf-8')
13+
for x in info.findall(data):
14+
for i in range(len(x)):
15+
sheet.write(line,i,x[i])
16+
line+=1
17+
book.save('student.xls')

yefan/014/14.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
#导入模块
5+
import simplejson as json
6+
import xlwt
7+
8+
#从文件(JSON形式)中读取数据返回字典
9+
def read_file(filename):
10+
with open(r'C:\Python27\oneday_one\student.txt','r') as fp:
11+
content = fp.read().decode('gbk').encode('utf-8')
12+
#print type(content)
13+
#simplejson这个模块还没细看,怎么解码还是需要了解下
14+
d = json.JSONDecoder().decode(content)
15+
#d=json.loads(content)
16+
return d
17+
18+
#生成对应的xls文件
19+
def gen_xls(d,filename):
20+
fp = xlwt.Workbook()
21+
table = fp.add_sheet('student',cell_overwrite_ok=True)
22+
#试了下,与很多要转utf-8(ASCII码)存文件的情况不同,xls不接受ASCII码形式的存储,直接用字典里面的Unicode就行了,简直好评,不用在特意decode或者encode了
23+
#想写得更加自动化一些,好复用.本身不太想用两层循环的,不过也不知道有没有更便捷的存储方式(比如整行自动匹配导入,算法是背后优化封装好的,就用了万能的这种方法)
24+
for n in range(len(d)):
25+
table.write(n,0,n+1)
26+
m = 0
27+
for record in d[str(n+1)]:
28+
table.write(n,m+1,record)
29+
m += 1
30+
fp.save('student.xls')
31+
print u'写入完毕'
32+
33+
#主函数,嘛,最后还是用“丑陋的二重循环”实现了,但是其实也没什么,还是要看场景和优化,毕竟这也不是做查找或者排序,在日常使用中也不用太担心性能问题
34+
def main():
35+
filename = 'student.txt'
36+
xls_name = 'student.xls'
37+
d = read_file(filename)
38+
gen_xls(d,xls_name)
39+
40+
if __name__ == '__main__':
41+
main()

0 commit comments

Comments
 (0)