0. 请写下这一节课你学习到的内容:格式不限,回忆并复述是加强记忆的好方式!
•任务:将文件(record.txt)中的数据进行分割并按照以下规律保存起来:
–1.小甲鱼的对话单独保存为boy_.txt的文件(去掉“小甲鱼:”) –小客服的对话单独保存为girl_.txt的文件(去掉“小客服:”)
2. –文件中总共有三段对话,分别保存为boy_1.txt, girl_1.txt,boy_2.txt, girl_2.txt,
boy_3.txt, gril_3.txt共6个文件(提示:文件中不同的对话间已经使用“==========”分割)
小客服:小甲鱼,今天有客户问你有没有女朋友?
小甲鱼:咦??
小客服:我跟她说你有女朋友了!
小甲鱼:。。。。。。
小客服:她让你分手后考虑下她!然后我说:"您要买个优盘,我就帮您留意下~"
小甲鱼:然后呢?
小客服:她买了两个,说发一个货就好~
小甲鱼:呃。。。。。。你真牛!
小客服:那是,谁让我是鱼C最可爱小客服嘛~
小甲鱼:下次有人想调戏你我不阻止~
小客服:滚!!!
================================================================================
小客服:小甲鱼,有个好评很好笑哈。
小甲鱼:哦?
小客服:"有了小甲鱼,以后妈妈再也不用担心我的学习了~"
小甲鱼:哈哈哈,我看到丫,我还发微博了呢~
小客服:嗯嗯,我看了你的微博丫~
小甲鱼:哟西~
小客服:那个有条回复“左手拿著小甲魚,右手拿著打火機,哪裡不會點哪裡,so easy ^_^”
小甲鱼:T_T
================================================================================
小客服:小甲鱼,今天一个会员想找你
小甲鱼:哦?什么事?
小客服:他说你一个学生月薪已经超过12k了!!
小甲鱼:哪里的?
小客服:上海的
小甲鱼:那正常,哪家公司?
小客服:他没说呀。
小甲鱼:哦
小客服:老大,为什么我工资那么低啊??是时候涨涨工资了!!
小甲鱼:啊,你说什么?我在外边呢,这里好吵吖。。。。。。
小客服:滚!!!
def save_file(boy, girl, count):
file_name_boy = 'boy_' + str(count) + '.txt'
file_name_girl = 'girl_' + str(count) + '.txt'
boy_file = open(file_name_boy, 'w')
girl_file = open(file_name_girl, 'w')
boy_file.writelines(boy)
girl_file.writelines(girl)
boy_file.close()
girl_file.close()
def split_file(file_name):
f = open(file_name)
boy = []
girl = []
count = 1
for each_line in f:
if each_line[:6] != '======':
(role, line_spoken) = each_line.split(':', 1)
if role == '小甲鱼':
boy.append(line_spoken)
if role == '小客服':
girl.append(line_spoken)
else:
save_file(boy, girl, count)
boy = []
girl = []
count += 1
save_file(boy, girl, count)
f.close()
split_file('record.txt')
## 0. 编写一个程序,接受用户的输入并保存为新的文件,程序实现如图:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200726180208167.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NhdHJ0ZWVz,size_16,color_FFFFFF,t_70)
```python
def writeFile(path):
f=open(path,'w')
userDate='请输入数据[单独输入quit结束]:'
print(userDate)
while userDate!='quit':
userDate=input("")
f.write(userDate)
f.close()
path=input("请输入文件路径:")
writeFile(path)
1. 编写一个程序,比较用户输入的两个文件,如果不同,显示出所有不同处的行号与第一个不同字符的位置,程序实现如图:
def compare(file1,file2):
f1=open(file1,encoding='UTF-8')
f2=open(file2,encoding='UTF-8')
count=0
differ=[]
while True:
str1=f1.readline()
str2=f2.readline()
count+=1
if str1=='' or str2=='':
break;
if str1!=str2:
differ.append(count)
return differ
file1=input("输入第一个文件路径:")
file2=input("输入第二个文件路径:")
print(compare(file1,file2))
2. 编写一个程序,当用户输入文件名和行数(N)后,将该文件的前N行内容打印到屏幕上,程序实现如图:
def printNLine(path,N):
Fread=open(path,encoding='UTF-8')
for i in range(N):
print(Fread.readline(),end='')
Fread.close()
path,N=input("输入文件路径和N(逗号隔开):").split(",")
printNLine(path,int(N))
3. 呃,不得不说我们的用户变得越来越刁钻了。要求在上一题的基础上扩展,用户可以随意输入需要显示的行数。
def selectLine(path,begin,end):
f=open(path,encoding='UTF-8')
if begin=='':
begin=0
if end=='':
end=1000
begin=int(begin)
end=int(end)
len=end-begin+1
if begin>0:
for i in range(begin):
f.readline()#读取begin前面的行
line=' '
while len>0 and line!='':
line=f.readline()
print(line,end='')
len-=1
f.close()
path=input("输入路径:")
begin,end=input("输入显示范围(如如3:6):").split(':')
selectLine(path,begin,end)
4. 编写一个程序,实现“全部替换”功能,程序实现如图:
def replace_word(path,new,old):
f=open(path)
txt=f.read()
count=txt.count(old)
print(count)
print(txt)
txt=txt.replace(old,new)
print(txt)
f=open(path,'w')
f.writelines(txt)
f.close()
path=input("请输入路径:")
replace_word(path,'改','人')