github/python/ show me the code 25題(一)


先上網址 https://github.com/Show-Me-the-Code/show-me-the-code

初學python拿來練手,記住一些常用的庫和函數

第 0000 題:將你的 QQ 頭像(或者微博頭像)右上角加上紅色的數字,類似於微信未讀信息數量那種提示效果。 類似於圖中效果

頭像

 1 # -*- coding:utf-8 -*-
 2 import sys
 3 import glob,os
 4 from PIL import Image,ImageDraw,ImageFont
 5 type = sys.getfilesystemencoding()
 6 
 7 font = ImageFont.truetype("msyhbd.ttc",30)
 8 for infile in glob.glob("*.jpg"):
 9     file,ext = os.path.splitext(infile)
10     im = Image.open(infile)
11     w,h = im.size
12     x=w*0.8
13     y=h*0.1
14     drawSurface = ImageDraw.Draw(im)
15     drawSurface.text((x,y), "4", fill=(255,0,0), font=font)
16     im.show()

這個題帶來了圖像處理的庫:PIL

Image和ImageDraw,ImageFont是配套的,Draw可以作畫,font可以添加一些東東比如字體

可能比較麻煩的是字體的名字怎么找……打開字體文件夾,隨便選中一款字體

右鍵,選擇屬性查看名稱。如果右鍵沒有屬性,說明這個字體里還分了類。。比如我的微軟雅黑里面還有細體粗體什么的。。打開再屬性就好

 

第 0001 題:做為 Apple Store App 獨立開發者,你要搞限時促銷,為你的應用生成激活碼(或者優惠券),使用 Python 如何生成 200 個激活碼(或者優惠券)?

激活碼,就是平時我們看到的那種很奇葩的亂七八糟的碼,數字夾雜了字母

而激活碼要保證獨一而二,又要考慮存儲於數據庫,那么必然要和id對應上。

# coding:utf-8
import random
import string

def create_code(id,length=10):
    id_ = hex(id)
    id_ = id_[2:]+'L'
    length = length-len(id_)
    chars=string.ascii_letters+string.digits;
    code=id_+ ''.join([random.choice(chars) for i in range(length)])
    return code

def get_Id(code):
    id_ = code.split('L')[0]
    id = int(id_,16)        
    return str(id)

if __name__ == "__main__":
    for i in range(0,200):
        code = create_code(i)
        id = get_Id(code)
        print code,id


#本篇重點是string中的char列表,還有join,random.choice(chars)用法

根據輸出可以看到I與id是相同的,把i當作id生成code,把code轉成id說明不變。成功~

chars列表很有意思~原來還能這么玩

第 0002 題:將 0001 題生成的 200 個激活碼(或者優惠券)保存到 MySQL 關系型數據庫中。

第 0003 題:將 0001 題生成的 200 個激活碼(或者優惠券)保存到 Redis 非關系型數據庫中。

這兩題涉及到數據庫~~博主並不會數據庫~~等我學了再寫這兩題咯

第 0004 題:任一個英文的純文本文件,統計其中的單詞出現的個數。

# -*-coding:utf-8 -*—

filename = "image.py"
key = "import"
file = open(filename)

text = file.read()
beg = 0
num = 0

while text.find(key) != -1:

    num = num + 1
    text = text.split(key,1)[1]
print num
file.close()

filename和key都是可以自己改的~~~~

這里需要注意的是find()函數。find()如果找得到,就會返回index,如果找不到,就會返回-1.所以千萬不可以直接寫 if find().無論如何都有返回值……所以一定要寫!=-1才能做出正確判斷

第 0005 題:你有一個目錄,裝了很多照片,把它們的尺寸變成都不大於 iPhone5 分辨率的大小。

# -*- coding:utf-8 -*-
import glob,os
from PIL import Image

size = 200,200
for file in glob.glob('*.jpg'):
    filename,ext = os.path.splitext(file)
    im = Image.open(file)
    w,h = im.size

    im.thumbnail(size)
    im.save(filename + 'thumbnail.jpg','JPEG')

    

os.path.splitext()函數會返回兩個部分,前一個是文件名(可能含路徑),后一個是類型名,glob.glob("")會查找當前目錄中符合的文件。如果想獲取當前的目錄,可以用os.getcwd()。python中類似的方法實在太多了

第 0006 題:你有一個目錄,放了你一個月的日記,都是 txt,為了避免分詞的問題,假設內容都是英文,請統計出你認為每篇日記最重要的詞。

最重要的詞,我就把它當出現最多的詞咯

# coding:utf-8
import os
import sys
import re
from collections import Counter
type = sys.getfilesystemencoding()


def getCounter(file):
    f = open(file)
    r = re.findall(r'[A-Za-z]+',f.read())
    return Counter(r)

if __name__ == "__main__":
    total_counter = Counter()
    for infile in os.listdir(os.getcwd()):
        filename,ext = os.path.splitext(infile)
        if ext == '.py':
#            total_counter += getCounter(infile)
            print filename    
            print getCounter(infile).most_common(2)

#    print total_counter.most_common()[0][0]

這次用到的是神奇的counter!counter類會對內部的數據進行自動統計計數,http://www.pythoner.com/205.html 這個網址有詳細的講解

第 0007 題:有個目錄,里面是你自己寫過的程序,統計一下你寫過多少行代碼。包括空行和注釋,但是要分別列出來。

#coding:utf-8
import os
import re
from collections import Counter

annotation = r'^\#.'
space = r'^\s+$'

if __name__ == "__main__":
    for i in os.listdir(os.getcwd()):
        code,blank,note = 0,0,0
        filename,ext = os.path.splitext(i)
        if ext == '.py':
            with open(i) as f:
                line = f.readline()
                while line:
                    if re.match(space,line):
                        blank += 1
                    elif re.match(annotation,line):
                        note += 1
                    else:
                        code += 1

                    line = f.readline()
        print filename
        print "code:%d, blank:%d, note:%d"%(code,blank,note)

同樣還是在當前目錄下運行……定義了注釋和空格符的正則,由於不會寫代碼的正則干脆用else來表示了,與c不同的是else if寫成elif……

re.match()就是捕獲咯,第一個參數是正則式,第二個參數就是看你檢驗的字符串啦

第 0008 題:一個HTML文件,找出里面的正文

#coding:utf-8
import glob,os
import requests
from bs4 import BeautifulSoup
import sys
ty = sys.getfilesystemencoding()
url = "https://github.com/Show-Me-the-Code/python/blob/master/Forec/0008/0008.py"
html = requests.get(url)
soup = BeautifulSoup(html.text,"html.parser")
print soup.body.text.encode('GBK','ignore').decode('GBK')

鼎鼎大名的bs4!!!!

曾經初學爬蟲………………用urllib爬下來然后partition...index....雖然也能做出效果然而實在蛋疼

beautifulsoup會把html代碼結構化。soup.body.text指的就是body的全部字符咯,當然也可以嘗試輸出soup.body.div……不過這樣輸出的是不含子結構的div,否則是檢查不到的~~~那么要怎么才能明確輸出某個藏在很深很深結構里標簽呢?下面就是這個啦

第 0009 題:一個HTML文件,找出里面的鏈接。換成<a>標簽去找。。。就好

第 0013 題: 用 Python 寫一個爬圖片的程序,爬 這個鏈接里的日本妹子圖片 :-)

#coding:utf-8
import requests
import urllib2
from bs4 import BeautifulSoup
import lxml.html
url = "http://tieba.baidu.com/p/2166231880"

html = requests.get(url)
soup = BeautifulSoup(html.text,"html.parser")
lis =  soup.find_all('img')
count = 1
for img in lis:
    src = img.attrs['src']
    start = src.rfind('/')+1
    end = src.rfind('?') if src.rfind('?') != -1 else len(src)
    name = src[start:end]
    #print name
    filepath = 'e:/image/'+str(count)+name
    with open(filepath,'wb') as f:
    #    image_data = urllib2.urlopen(src).read() #bs4這里好像下不了
        image_data = requests.get(src).content #bs4的用法
        f.write(image_data)
    count += 1

注意find_all函數。。。。不管這個img標簽藏得多么深。。這么寫就會通通把他們找出來啦

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM