#打开和关闭文件
'''
f=open(r'..\Stu_pack\Dream It Possible.txt','rt',encoding='utf_8')
for line in f:
print(line)
f.close()
'''
#写入和读取文件
#(将Stu_pack里面的'万疆.txt'写入demo里面'副本_万疆.txt')
'''
fr=open('..\Stu_pack\万疆.txt','rt',encoding='utf_8')
fw=open('副本_万疆.txt','w+',encoding='utf_8')
for line in fr:
fw.write(line)
fr.close()
fw.close()
'''
#复制和转换文件
#CSV-CSV
'''
fr=open('..\Stu_pack\学生考勤.csv','rt')
fw=open('副本_学生考勤.csv','w+')
ls=[]
for line in fr:
line=line.replace('\n','') #删除每一行后面的换行符
line=line.split(',') #将每行数据以逗号分割
ls.append(line) #将分割的每个数据追加到列表内
print(ls)
for row in ls:
fw.write(','.join(row)+'\n') #将每一个数据之间添加逗号,并且每行后面添加换行符
print(row)
fr.close()
fw.close()
'''
#CSV-TXT
#P147-See You Again统计频次
import re
fp=open('..\Stu_pack\See You Again.txt','r')
txt=fp.read()
fp.close()
txt=txt.lower()
txt=re.sub('[,.?:"\'!-]','',txt)
words=txt.split()
word_sq={}
for each in words:
if each not in word_sq.keys():
word_sq[each]=1
else:
word_sq[each]=word_sq[each]+1
print(word_sq)
res=sorted(word_sq.items(),key=lambda x:x[1],reverse=Ture)
print(res)