Time of Update: 2018-07-29
這個模組提供了一些簡單的介面來對檔案進行壓縮和解壓縮 類似於GNU項目的gzip和gunzip,資料的壓縮源於zlib模組的支援。 gzip.open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None) 開啟一個gzip已經壓縮好的gzip格式的檔案,並返回一個檔案對象:file object. 參數filename可以是真是的檔案名稱(a str or bytes對象),
Time of Update: 2018-07-29
轉自:http://blog.csdn.net/ysdaniel/article/details/7970883 eg: [python] view plain copy #! /usr/bin/python import sys
Time of Update: 2018-07-29
difflib模組提供的類和方法用來進行序列的差異化比較,它能夠比對檔案並產生差異結果文本或者html格式的差異化比較頁面,如果需要比較目錄的不同,可以使用filecmp模組。 class difflib.SequenceMatcher
Time of Update: 2018-07-29
程式如下: for line in file.readlines(): line=line.strip('\n') 使用strip()函數去掉每行結束的\n strip()函數原型 聲明:s為字串,rm為要刪除的字元序列 s.strip(rm) 刪除s字串中開頭、結尾處,位於 rm刪除序列的字元 s.lstrip(rm) &
Time of Update: 2018-07-29
file1=input("請輸入需要開啟的檔案名稱:")+".txt"f1=open(file1,"r")for line1 in f1.readlines(): line1=line1.strip("\n") print (line1) 擴充:比較使用者輸入的兩個檔案,如果不同,顯示出所有不同處的行號與第一個不同字元的位置。 def file_compare(file1,file2):
Time of Update: 2018-07-29
函數名其實就是指向一個函數對象的引用,完全可以把函數名賦給一個變數,相當於給這個函數起了一個“別名”: >>> a = abs # 變數a指向abs函數>>> a(-1) # 所以也可以通過a調用abs函數#!/usr/bin/env python3# -*- coding: utf-8 -*-x = abs(100)y = abs(-20)print(x, y)print('max(1, 2, 3) =', max(1, 2, 3))print('
Time of Update: 2018-07-29
5 Python常用資料結構 Table of Contents 1 List 1.1 List 的使用 1.2 將List當作棧(Stack) 1.3 將List當作隊列 1.4 函數式程式設計工具 1.5 List Comprehensions 2 del 語句 3 Tuples 和 序列 4
Time of Update: 2018-07-29
<pre name="code" class="python">解壓gzip檔案樣本:import gzipf = gzip.open('file.txt.gz', 'rb')file_content = f.read()f.close()建立gzip檔案:import gzipcontent = "Lots of content here"f = gzip.open('file.txt.gz', 'wb')f.write(
Time of Update: 2018-07-29
3 Python非正式導引 在本節的例子中,以提示符>>>, … ,開始的是輸入,否則為輸出, #後為python的注釋 Table of Contents 1 把Python當作計算機 1.1 數字 1.2 字串 1.3 Unicode 編碼的字串 1.4 List 2 初步程式設計
Time of Update: 2018-07-29
#!/usr/bin/env python# -*- coding: utf-8 -*-# sum的最常見用法計算一個序列的累加和print(sum([1, 2, 3]))# 6# # sum 函數原型是 sum(iterable, start), 還可以給個初始值print(sum([1, 2, 3], 10))# 16# 另外一個比較Hack一些的用法, 展開2層的嵌套列表iterable = [[1, 2], [3, 4], [5, 6], [7, 8]]start = [0]print(
Time of Update: 2018-07-29
字串格式化-format() 轉載請標明出處(http://blog.csdn.net/lis_12/article/details/52712994). 普通格式化方法 (%s%d)產生格式化的字串,其中s是一個格式化字串,d是一個十進位數; 格式化字串包含兩部分:普通的字元和轉換說明符(見下表), 將使用元組或映射中元素的字串來替換轉換說明符; **如果d是元組的話,必須與s中的轉換說明符個數一致;
Time of Update: 2018-07-29
#壓縮軟體#設計介面import tkinterimport tkinter.filedialogimport zipfileimport osimport tkinter.messageboxroot = tkinter.Tk()root.title('壓縮軟體1.0')root.minsize(300,400)#設定需要壓縮路徑變數zipfilename = []#添加檔案的函數def addfile(): #全域化變數 global zipfilename
Time of Update: 2018-07-29
檔案內容對比 #!/usr/bin/python import difflib text1 = """text1: This module provides classes and functions for comparing sequences. including HTML and context and unified diffs. difflib
Time of Update: 2018-07-29
問題: 我有這樣的一個列表: 1 ['a.b.c.d11u.e.f.g', 'e.f88.g', 'caa3.z.brr', 'z.48.ff.ee'] 需要找節點最多的一個(節點間由.分割) 看似簡單的工作,要用 Pythonic 的方法來做,還是要對 Python 的內建函數有一定程度的熟悉,比如這裡可以用最熟悉不過的max,但是會用到它並不常用的選擇性參數:key
Time of Update: 2018-07-29
#!/usr/bin/env python# -*- coding:utf-8 -*-# 1、方案1:使用內建函數sortedfrom random import randintd = {x: randint(60, 100) for x in 'xadfyz'}# 將字典轉換為包含元組的list,使用zip函數 結果如:[(60, 'a'), (95, 'd'), (95, 'f'), (84, 'y'), (66, 'x'), (82, 'z')]# result =
Time of Update: 2018-07-29
shell與python間傳遞變數方法 (2013-02-12 18:02:47) 轉載▼ 分類: shell_vim python -> shell: 1.環境變數 [python]
Time of Update: 2018-07-29
我們談到“文本處理”時,我們通常是指處理的內容。Python 將文字檔的內容讀入可以操作的字串變數非常容易。檔案對象提供了三個“讀”方法: .read()、.readline() 和 .readlines()。每種方法可以接受一個變數以限制每次讀取的資料量,但它們通常不使用變數。 .read() 每次讀取整個檔案,它通常用於將檔案內容放到一個字串變數中。然而 .read() 組建檔案內容最直接的字串表示,但對於連續的面向行的處理,它卻是不必要的,並且如果檔案大於可用記憶體,則不可能實現這種處理。
Time of Update: 2018-07-29
Python中的類(上) 在Python中,可以通過class關鍵字定義自己的類,然後通過自訂的類對象類建立執行個體對象。 例如,下面建立了一個Student的類,並且實現了這個類的初始化函數"__init__": class Student(object): count = 0 books = [] def __init__
Time of Update: 2018-07-29
Python 支援一種有趣的文法,它允許你快速定義單行的最小函數。這些叫做 lambda 的函數,是從 Lisp 借用來的,可以用在任何需要函數的地方。 例 4.20. lambda 函數介紹 >>> def f(x):... return x*2... >>> f(3)6>>> g = lambda x: x*2
Time of Update: 2018-07-29
類-內建函數 服務於類以及對象。 9個內建函數 issubclass,isinstance,hasattr,getattr,setattr,delattr,dir,super,vars 注意:getattr()和delattr()可能拋出異常(如AttributeError),要結合try……expect來處理異常