文件分類 幾種文件分類的形式
阿新 • • 發佈:2018-07-23
utf-8 河南 list nes lose 重慶 imp 省份 上海
文件分類的一般流程
# 1,需要處理的文件 # 2.創建保存文件的列表 # 3.寫入文件 # 4.關閉文件
事例一.
QQlist=[5,6,7,8,9,10,11,"小垃圾"] filepath = r"G:\我的python\python基礎\day12\QQ\QQGood.txt" file = open(filepath,"rb") mylist = file.readlines() file.close() # 創建文件對象 存放到列表中 filelist=[] for i in QQlist: QQfilepath = "G:\\我的python\\python基礎\\day12\\QQ\\QQ位數分類\\"+str(i)+"位QQ.txt" QQfile = open(QQfilepath,"wb") filelist.append(QQfile) # 分類位置 for line in mylist: bakline = line line = line.decode("utf-8") linelist = line.split("----") length = len(linelist[0]) # 取賬號的長度 if length == 5: filelist[0].write(bakline) elif length == 6: filelist[1].write(bakline) elif length == 7: filelist[2].write(bakline) elif length == 8: filelist[3].write(bakline) elif length == 9: filelist[4].write(bakline) elif length == 10: filelist[5].write(bakline) elif length == 11: filelist[6].write(bakline)else: filelist[7].write(bakline) # 關閉文件 for QQfile in filelist: QQfile.close()
事例二
import codecs # 1,需要處理的文件 filepath = r"G:\我的python\尹成\python基礎\day12\kaifang\kaifangGood.txt" file = codecs.open(filepath,"rb","utf-8","ignore") mylist = file.readlines() file.close() print("內存加載結束,準備區域劃分") # 2.創建保存文件的列表 area = [ age for age in range(1000,2018)] length = len(area) areafilelist = [] for data in area: areafilepath = "G:\\我的python\\尹成\\python基礎\\day12\\kaifang\\年齡\\" + str(data) + ".txt" areafile = open(areafilepath,"wb") areafilelist.append(areafile) # 3.寫入文件 for line in mylist: linelist = line.split(",") chstr = linelist[1][6:10] # 取出一個字符 for i in range(length): if str(area[i]) == chstr: areafilelist[i].write(line.encode("utf-8")) # 4.關閉文件 for data in areafilelist: data.close()
事例三 (按照身份證號碼取)
import codecs filepath = r"G:\我的python\python基礎\day12\kaifang\kaifangGood.txt" file = codecs.open(filepath,"rb","utf-8","ignore") mylist = file.readlines() file.close() print("內存加載結束,準備區域劃分") # 分類器 area=[[11,"北京"],[12,"天津"],[13,"河北"],[14,"山西"],[15,"內蒙古"],[21,"遼寧"],[22,"吉林"],[23,"黑龍江"],[31,"上海"],[32,"江蘇"],[33,"浙江"],[34, "安徽"],[35,"福建"],[36,"江西"],[37,"山東"],[41,"河南"],[42,"湖北"], [43, "湖南"],[44,"廣東"],[45,"廣西"],[46,"海南"],[50,"重慶"], [51, "四川"],[52,"貴州"],[53,"雲南"],[54,"西藏"],[61,"陜西"], [62, "甘肅"],[63,"青海"],[64,"寧夏"],[65,"新疆"],[71,"臺灣"], [81, "香港"],[82,"澳門"]] length=len(area)#長度 # 創建區域文件的對象 areafilelist = [] for data in area: kffilefath = "G:\\我的python\\python基礎\\day12\\kaifang\\省份\\" + data[1] + ".txt" kffile = open(kffilefath,"wb") areafilelist.append(kffile) for line in mylist: linelist = line.split(",") # 字符串切割 chstr = linelist[1][0:2] # 取出月份 # 判斷身份證所在的地區 for i in range(length): if str(area[i][0]) == chstr: areafilelist[i].write(line.encode("utf-8")) break # 關閉文件 for line in areafilelist: line.close() length=len(area)#長度 # 創建區域文件的對象 areafilelist = [] for data in area: kffilefath = "G:\\我的python\\尹成\\python基礎\\day12\\kaifang\\區域\\" + data[1] + ".txt" kffile = open(kffilefath,"wb") areafilelist.append(kffile) # 分類寫入數據 for line in mylist: linelist = line.split(",") # 字符串切割 chstr = linelist[1][0:2] # 取出2個字符 # 判斷身份證所在的地區 for i in range(length): if str(area[i][0]) == chstr: areafilelist[i].write(line.encode("utf-8")) break # 關閉文件 for line in areafilelist: line.close()
文件分類 幾種文件分類的形式