《可愛的Python》讀書筆記(八)

來源:互聯網
上載者:User

標籤:stack   chardet   

問題的最佳的解決方案,就是找到那段別人解決相似問題的代碼。


今天做些能回顧所學知識點的小練習,類似的問題參考別人的代碼,修改成自己容易理解的模樣。

1、實現簡單的棧。put(item)實現資料item插入棧中;get()實現從棧中取一個資料。

# -*- coding: utf-8 -*-class MyStack(object):    '''MyStack        自訂棧,操作有put(), get()    '''    def __init__(self):        self.head = -1        self.stack = []        def put(self, item):        self.head += 1        self.stack.append(item)        print('Put %s Success' % item)        def get(self):        if self.head < 0:            return "Put Error: The Stack is Overflow!"        else:            self.head -= 1            return self.stack.pop()            def isEmpty(self):        return self.item == []                if __name__ == "__main__":    mystack = MyStack()    mystack.put('a')    mystack.put('b')    print(mystack.get())    mystack.put('c')    print(mystack.get())    print(mystack.get())    print(mystack.get())

運行結果如下:

Put a SuccessPut b SuccessbPut c SuccesscaPut Error: The Stack is Overflow!


2、判斷指定網頁使用的編碼。

# -*- coding: utf-8 -*-import sysimport requestsimport chardetdef web_detect(url):        #檢測網頁的編碼方式        try:        response = requests.get(url)    except:        print('error')        return 0    web= response.content    response.close()    codedetect = chardet.detect(web)["encoding"]    print('%s\t<-\t%s' % (url, codedetect))    return 1    if __name__ == '__main__':    if len(sys.argv) == 1:        print('usage:\n\tpython XX.py http://xxx.com')    else:        web_detect(sys.argv[1])

運行效果如下:

C:\>python webdetect.py http://blog.51cto.com/9473774http://blog.51cto.com/9473774   <-      utf-8C:\>python webdetect.py http://www.163.comhttp://www.163.com      <-      GB2312


3、遍曆指定目錄下的所有檔案,找出其中佔用空間最大的前3個檔案。

# -*- coding: utf-8 -*-import sysimport osdef get_top_three(path):        d = {}    for root, dirs, files in os.walk(path):        for file in files:            fname = os.path.join(root, file)            fsize = os.stat(fname).st_size            d[fname] = fsize    #print(d)    f = sorted(zip(d.values(), d.keys()))    for i in [-1, -2, -3]:        for j in f[i]:            print(j)                        get_top_three('E:\\iso\\CentOS-6.8-x86_64-bin-DVD1')

運行結果如下:

146313216E:\iso\CentOS-6.8-x86_64-bin-DVD1\images\install.img45373440E:\iso\CentOS-6.8-x86_64-bin-DVD1\images\efidisk.img40688737E:\iso\CentOS-6.8-x86_64-bin-DVD1\isolinux\initrd.img


《可愛的Python》讀書筆記(八)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.