import codecs
path = r'C:\Users\旭\Desktop\kaifang3.txt'
path1= r'C:\Users\旭\Desktop\read\\'
f = codecs.open(path,'rb',encoding='utf-8',errors='ignore')
radlines = f.readlines()
f.close()
#创建 地区的列表
arealist = [[1,'华北'],[2,'东北'],[3,'华东'],[4, '中南'],[5,'西南'],[6,'西北']]
areaio=[]
#创建文件for i in arealist:
f = open(path1+i[1]+'.txt','wb')
areaio.append(f)
#写入Filefor i in radlines:
listinfo = i.split(',')
target = listinfo[1][0]
for j in range(len(arealist)):
if int(target) == arealist[j][0]:
areaio[j].write(i.encode('utf-8'))
break#关闭流for k in areaio:
k.close()
2. 分类出生年
import codecs
#地址
path = r'C:\Users\旭\Desktop\kaifang3.txt'
path1= r'C:\Users\旭\Desktop\read\\'#获取 每行内容
of = codecs.open(path,'rb',encoding='utf-8',errors='ignore')
oflist = of.readlines()
of.close()
yearlist=[]
#获取年份for year in oflist:
infolist = year.split(',')[1]
target = infolist[6:10]
if int(target[0]) ==1:
yearlist.append(target)
yearlist = list(set(yearlist))
#保存在list中流
fileIO=[]
for filename in yearlist:
fw = open(path1+filename+'.txt','wb')
fileIO.append(fw)
#判断相应分类 写入for er in oflist:
infolist = er.split(',')[1]
target = infolist[6:10]
for i in range(len(yearlist)):
if target == yearlist[i]:
fileIO[i].write(er.encode('utf-8'))
break#关闭流for i in fileIO:
i.close()
Pickle
1. 写入文件
f = open('pickle.txt','wb')
mylist = [['a','b'],[1,2,3]]
pickle.dump(mylist,f)
f.close()
2.读取列表文件
f = open('pickle.txt','rb')
print(pickle.load(f))
OS文件模块
import os
#操作系统 nt = windows
print(os.name)
#创建文件夹
os.mkdir('Bruce')
#删除文件夹# os.rmdir('新文件夹')#获取当前工作目录
print(os.getcwd())
#重命名
print(os.rename('Chen','Bruce'))
#凭借路径
print(os.path.join('路径','a.txt'))
#判断是否是文件
print(os.path.isfile('路径'))
#返回大小
print(os.path.getsize('路径'))