pythonextractall
『壹』 python 數據處理(三十六)—— 文本數據處理(續)
可以使用 [] 符號直接按位置進行索引,如果索引超過字元串的長度,結果將是 NaN
在 0.23 版本之前, extract 方法的參數 expand 默認為 False 。當 expand=False 時, expand 會根據正則表達式模式返回一個 Series 、 Index 或 DataFrame
當 expand=True 時,它總是返回一個 DataFrame ,這種方式更加符合用戶的需求,從 0.23.0 版本開始就是默認的
extract 方法接受一個至少包含一個捕獲組的正則表達式
如果是包含多個組的正則表達式將返回一個 DataFrame ,每個捕獲組是一列
未匹配的行會填充 NaN ,可以從混亂的字元串序列中提取出有規則的信息。
對於命名分組
對於可選的分組
注意 :正則表達式中的任何捕獲組名稱都將用作列名,否則將使用捕獲組號
如果 expand=True ,則返回一個 DataFrame
如果 expand=False ,則返回一個 Series
對於索引,如果 expand=True ,且只有一個捕獲組則返回一個只有一列的 DataFrame
此時,如果 expand=False 將會返回一個 Index
對於索引,正則表達式設置多個分組將返回 DataFrame
如果 expand=False 將會拋出 ValueError 異常
對於 extract 只返回第一個匹配項
與 extract 不同, extractall 方法返回每個匹配項,其結果始終是具有 MultiIndex 的 DataFrame 。
MultiIndex 的最後一級名為 match ,標示的是匹配的順序
對於只有一個匹配的 Series
extractall(pat).xs(0, level='match') 與 extract(pat) 的結果一致
Index 也支持 .str.extractall ,它返回一個 DataFrame ,其結果與 Series.str 相同。
您可以檢查字元串元素中是否包含正則匹配模式
或者字元串元素是否與模式匹配
而在 1.1.0 版本中
注意 :
match 、 fullmatch 和 contains 之間的區別是:
這三個函數於 re 模塊的 re.fullmatch 、 re.match 和 re.search 對應
像 match , fullmatch , contains , startswith 和 endswith 有一個額外的 na 參數,用於將缺失值替換為 True 或 False
您可以從字元串列中提取指標變數。例如,如果使用 '|' 分隔的字元串
字元串 Index 也支持 get_mmies ,它返回一個 MultiIndex
『貳』 python怎樣解壓.tar.gz的文件
linux:
os.system('tar zxf %s' % filename )
window:
import tarfile
tar = tarfile.open("sample.tar.gz")
tar.extractall()
tar.close()
『叄』 python 3.2版本 解壓rar/zip到指定目錄
python沒有rar模塊的
『肆』 Python:invalid syntax的問題
我給你改了一下:
importzipfile
fromthreadingimportThread
defextractFile(zFile,password):
try:
zFile.extractall(pwd=password.encode())
print('password:'+password+' ')
zFile.close()
exceptExceptionase:
pass
defmain():
zFile=zipfile.ZipFile('evil.zip')
passFile=open('dict.txt')
lines=passFile.readlines()
passFile.close()
forlineinlines:
password=line.strip(' ')
t=Thread(target=extractFile,args=(zFile,password))
t.start()
if__name__=='__main__':
main()
要學習異常處理,參考這個教程吧:劉江的博客及教程
『伍』 python中的zipfile
python中的zipfile模塊是用於解壓/壓縮zip文件的,壓縮a.txt為a.zip,從b.zip解壓出b.txt(假設它們都放在d:\,且b.zip中只有b.txt)示範代碼如下5行:
from os import chdir;
from zipfile import ZipFile;
chdir("d:/");
with Zipfile("d:/b.zip","r") as zipf:zipf.extractall();
with Zipfile("d:/a.zip","w") as zipf:zipf.write("d:/a.txt");