Python文字操作---資料清洗
阿新 • • 發佈:2019-01-23
1.匯入編碼模組:codecs
2.資料清洗:把資料進行處理分類,可進行讀寫到文字上或者資料庫上
3.split():對特定的子串進行切割
import codecs filepath=r"Z:\F\第一階段視訊\20170424\vedio\大資料相關資料\1E~001.txt" file=codecs.open(filepath,"rb","gbk","ignore")#按照指定編碼 mylist=file.readlines()#返回一個list,讀取到記憶體 savegoodfilepath="好的資料儲存地址 savebadfilepath="錯誤的資料儲存地址" filegood=open(savegoodfilepath,"wb") filebad=open(savebadfilepath,"wb") if __name__ == '__main__': for line in mylist: if len(line)>35 or len(line)<15: filebad.write(line.encode("utf-8")) else: QQlist = line.split('----') if len(QQlist)==2: filegood.write(line.encode("utf-8")) else: filebad.write(line.encode("utf-8")) filebad.close() filegood.close()