這篇文章主要介紹了python文件數(shù)據(jù)分析治理提取的方法是什么的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇python文件數(shù)據(jù)分析治理提取的方法是什么文章都會有所收獲,下面我們一起來看看吧。
創(chuàng)新互聯(lián)建站是一家專業(yè)提供開平企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計、網(wǎng)站制作、HTML5、小程序制作等業(yè)務(wù)。10年已為開平眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。
python2.0有無法直接讀取中文路徑的問題,需要另外寫函數(shù)。python3.0在2018年的時候也無法直接讀取。
現(xiàn)在使用的時候,發(fā)現(xiàn)python3.0是可以直接讀取中文路徑的。
需要自帶或者創(chuàng)建幾個txt文件,里面最好寫幾個數(shù)據(jù)(姓名,手機(jī)號,住址)
寫代碼的時候最好,自己設(shè)幾個要求,明確下目的:
需要讀取對應(yīng)目錄路徑的所有對應(yīng)文件
按行讀取出每個對應(yīng)txt文件的記錄
使用正則表達(dá)式獲取每行的手機(jī)號
將手機(jī)號碼存儲到excel中
1)讀取文件
2)讀取數(shù)據(jù)
3)數(shù)據(jù)整理
4)正則表達(dá)式匹配
5)數(shù)據(jù)去重
6)數(shù)據(jù)導(dǎo)出保存
import glob import re import xlwt filearray=[] data=[] phone=[] filelocation=glob.glob(r'課堂實訓(xùn)/*.txt') print(filelocation) for i in range(len(filelocation)): file =open(filelocation[i]) file_data=file.readlines() data.append(file_data) print(data) combine_data=sum(data,[]) print(combine_data) for a in combine_data: data1=re.search(r'[0-9]{11}',a) phone.append(data1[0]) phone=list(set(phone)) print(phone) print(len(phone)) #存到excel中 f=xlwt.Workbook('encoding=utf-8') sheet1=f.add_sheet('sheet1',cell_overwrite_ok=True) for i in range(len(phone)): sheet1.write(i,0,phone[i]) f.save('phonenumber.xls')
會生成一個excel文件
import glob import re import xlwt
globe用來定位文件,re正則表達(dá)式,xlwt用于excel
filelocation=glob.glob(r'課堂實訓(xùn)/*.txt')
指定目錄下的所有txt文件
for i in range(len(filelocation)): file =open(filelocation[i]) file_data=file.readlines() data.append(file_data) print(data)
將路徑下的txt文件循環(huán)讀取,按序號依次讀取文件
打開每一次循環(huán)對應(yīng)的文件
將每一次循環(huán)的txt文件的數(shù)據(jù)按行讀取出來
使用append()方法將每一行的數(shù)據(jù)添加到data列表中
輸出一下,可以看到將幾個txt的文件數(shù)據(jù)以字列形式存在同一個列表
combine_data=sum(data,[])
列表合并成一個列表
print(combine_data) for a in combine_data: data1=re.search(r'[0-9]{11}',a) phone.append(data1[0]) phone=list(set(phone)) print(phone) print(len(phone))
set()函數(shù):無序去重,創(chuàng)建一個無序不重復(fù)元素集
#存到excel中 f=xlwt.Workbook('encoding=utf-8') sheet1=f.add_sheet('sheet1',cell_overwrite_ok=True) for i in range(len(phone)): sheet1.write(i,0,phone[i]) f.save('phonenumber.xls')
Workbook('encoding=utf-8'):設(shè)置工作簿的編碼
add_sheet('sheet1',cell_overwrite_ok=True):創(chuàng)建對應(yīng)的工作表
write(x,y,z):參數(shù)對應(yīng)行、列、值
關(guān)于“python文件數(shù)據(jù)分析治理提取的方法是什么”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“python文件數(shù)據(jù)分析治理提取的方法是什么”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享名稱:python文件數(shù)據(jù)分析治理提取的方法是什么
當(dāng)前網(wǎng)址:http://aaarwkj.com/article12/ihhhgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、網(wǎng)站制作、App設(shè)計、做網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)