学习目标:
这节课学习了用代码打开学生文件~
学习内容:
#文件
#1.文件的打开与关闭
'''
f=open('../Stu_pack/Dream It Possible.txt','rb')
for line in f:
print(line)
print(f.readline(6)) #处理文件
f.close() #关闭文件
'''
#2.文件的读写
'''
fw=open('test.txt','w+')
fw.write('上海电子信息职业业技术学院’)
fw.seek(0) #改变当前文件操作指针的位置到文件的开头
print(fw.read())
fw.close()
'''
#3.txt文件与CSV文件的转换
#CSV-->TXT
f_CSV=open('../Stu_pack/Stu_Exe204.csv','rt') #读取文件
ls=[]
for line in f_CSV:
#line=line.replace('\n','') #删除每行后面的换行符号‘\n’
ls.append(line) #在列表ls里面追加数据
print(ls)
f_CSV.close()
f_TXT=open('Stu_Exe204.csv','w+') #写入文件
for row in ls:
print(row)
f_TXT.write(','.join(row)+'\n') #将列表ls里面的数据写入TXT文件,数据前面添加
f_TXT.close()
序,号,,,学,号,,,姓,名,,,t,x,t,笔,记,,,文,件,夹,
1,,,2,0,2,0,1,1,0,2,0,1,0,1,2,,,陈,思,捷,,,优,秀,,,完,整,
2,,,2,0,2,0,1,1,0,2,0,1,0,3,4,,,古,莱,柯,孜,,,优,秀,,,完,整,
3,,,2,0,2,0,1,1,0,2,0,1,0,3,7,,,郭,金,,,优,秀,,,完,整,
4,,,2,0,2,0,1,1,0,2,0,1,0,4,1,,,何,涛,,,优,秀,,,完,整,
5,,,2,0,2,0,1,1,0,2,0,1,0,4,5,,,冀,明,瑞,,,优,秀,,,完,整,
6,,,2,0,2,0,1,1,0,2,0,1,0,5,3,,,李,佳,辉,,,优,秀,,,完,整,
7,,,2,0,2,0,1,1,0,2,0,1,0,5,5,,,李,杰,,,优,秀,,,完,整,
8,,,2,0,2,0,1,1,0,2,0,1,0,5,7,,,李,清,岚,,,优,秀,,,完,整,
9,,,2,0,2,0,1,1,0,2,0,1,0,6,3,,,李,泽,,,优,秀,,,冗,长,
1,0,,,2,0,2,0,1,1,0,2,0,1,0,6,4,,,李,政,积,,,优,秀,,,完,整,
1,1,,,2,0,2,0,1,1,0,2,0,1,0,7,3,,,刘,璐,,,优,秀,,,完,整,
1,2,,,2,0,2,0,1,1,0,2,0,1,0,8,0,,,马,凌,,,优,秀,,,冗,长,
1,3,,,2,0,2,0,1,1,0,2,0,1,0,8,8,,,彭,龙,军,,,优,秀,,,完,整,
1,4,,,2,0,2,0,1,1,0,2,0,1,0,9,0,,,祁,凤,阳,,,优,秀,,,完,整,
1,5,,,2,0,2,0,1,1,0,2,0,1,0,9,7,,,史,欣,怡,,,优,秀,,,完,整,
1,6,,,2,0,2,0,1,1,0,2,0,1,1,0,3,,,唐,哲,毅,,,优,秀,,,完,整,
1,7,,,2,0,2,0,1,1,0,2,0,1,1,0,4,,,陶,贤,方,,,优,秀,,,完,整,
1,8,,,2,0,2,0,1,1,0,2,0,1,1,0,7,,,涂,星,宇,,,良,好,,,完,整,
1,9,,,2,0,2,0,1,1,0,2,0,1,1,1,4,,,王,俊,成,,,优,秀,,,完,整,
2,0,,,2,0,2,0,1,1,0,2,0,1,1,1,5,,,王,奇,,,优,秀,,,完,整,
2,1,,,2,0,2,0,1,1,0,2,0,1,1,2,1,,,王,子,琛,,,优,秀,,,完,整,
2,2,,,2,0,2,0,1,1,0,2,0,1,1,3,0,,,熊,嘉,乐,,,优,秀,,,冗,长,
2,3,,,2,0,2,0,1,1,0,2,0,1,1,3,8,,,徐,永,漫,,,优,秀,,,冗,长,
2,4,,,2,0,2,0,1,1,0,2,0,1,1,5,0,,,姚,士,博,,,优,秀,,,完,整,
2,5,,,2,0,2,0,1,1,0,2,0,1,1,5,7,,,张,乐,星,,,优,秀,,,完,整,
2,6,,,2,0,2,0,1,1,0,2,0,1,1,5,9,,,张,攀,,,优,秀,,,完,整,
2,7,,,2,0,2,0,1,1,0,2,0,1,1,6,1,,,张,晟,,,优,秀,,,完,整,
2,8,,,2,0,2,0,1,1,0,2,0,1,1,6,6,,,张,楹,,,优,秀,,,冗,长,
2,9,,,2,0,2,0,1,1,0,2,0,1,1,6,8,,,张,云,杰,,,优,秀,,,完,整,
3,0,,,2,0,2,0,1,1,0,2,0,1,0,2,3,,,董,王,希,,,优,秀,,,冗,长,