解决代码:
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. 编写一个程序,接受用户的输入并保存为新的文件,程序实现如图:
答:
file_name = input('请输入文件名:')
f = open(file_name,'x')
print('请输入内容【单独输入‘:w’保存退出】:')
while 1:
file_line = input()
if file_line != ':w':
f.write(file_line + '\n')
else:
break
f.close()
1. 编写一个程序,比较用户输入的两个文件,如果不同,显示出所有不同处的行号与第一个不同字符的位置,程序实现如图:
答:
import os
file_name_1 = input('请输入需要比较的头一个文件名:')
file_name_2 = input('请输入需要比较的另一个文件名:')
f1 = open(file_name_1)
f2 = open(file_name_2)
size1 = os.path.getsize(file_name_1)
size2 = os.path.getsize(file_name_2)
count = 0
while 1:
count += 1
line_1 = f1.readline()
line_2 = f2.readline()
if line_1 != line_2:
length1 = len(line_1)
length2 = len(line_2)
if length1 > length2 :
line_2 = line_2 + ' '*(length1-length2)
else:
line_1 = line_1 + ' '*(length2-length1)
length = range(len(line_1))
for i in length:
if line_1[i] != line_2[i]:
print('第%d行不一样,从第%d个字符开始'%(count,i+1))
break
if f1.tell() == size1 or f2.tell() == size2:
break
2. 编写一个程序,当用户输入文件名和行数(N)后,将该文件的前N行内容打印到屏幕上,程序实现如图:
答:
file_name = input('请输入要打开的文件(C:\\test.txt):')
lines = int(input('请输入需要显示该文件前几行:\n'))
f = open(file_name)
print('文件'+file_name+'的前'+str(lines)+'行的内容如下:\n')
while lines:
print(f.readline())
lines -= 1
f.close()
3. 呃,不得不说我们的用户变得越来越刁钻了。要求在上一题的基础上扩展,用户可以随意输入需要显示的行数。(如输入13:21打印第13行到第21行,输入:21打印前21行,输入21:则打印从第21行开始到文件结尾所有内容)
答:
import os
file_name = input('请输入要打开的文件(C:\\test.txt):')
lines = input('请输入需要显示的行数【格式如 13:21 或 :21 或 21:】:')
start,end = lines.split(':')
f = open(file_name)
#检测文件的行数
size = os.path.getsize(file_name)
count = 0
while 1:
f.readline()
count += 1
if f.tell() == size:
break
f.seek(0,0)
if start != '' and end != '':
start = int(start)
end = int(end)
if 0 < start < end < count:
print('文件%s从第%s行到第%s行的内容如下:\n'%(file_name,start,end))
flag = start - 1
while flag:
f.readline()
flag -= 1
flag = end - start + 1
while flag:
print(f.readline())
flag -= 1
elif start == '' and end != '':
end = int(end)
if 0 < end < count:
print('文件%s从开始到第%s行的内容如下:\n'%(file_name,end))
while end:
print(f.readline())
end -= 1
else:
print('输入超出范围!')
elif end == ''and start != '':
start = int(start)
if 0 < start < count:
print('文件%s从第%s行到末尾的内容如下:\n'%(file_name,start))
flag = start - 1
while flag:
f.readline()
flag -= 1
while 1:
print(f.readline())
if f.tell() == size:
break
else:
print('输入超出范围!')
elif start == '' and end == '':
print('文件%s的全文内容如下:\n'%file_name)
print(f.read())
else:
print('输入不符合规范')
f.close()
答:
def update(part,new_word):
new_line = part[0]
for each_part in part[1:]:
new_line += new_word
new_line += each_part
return new_line
file_name = input('请输入文件名:')
old_word = input('请输入需要替换的单词或字符:')
new_word = input('请输入新的单词或字符:')
f = open(file_name,'r')
list1 = []
number = 0
for each_line in f:
list1.append(each_line)
number += each_line.count(old_word)
print('文件%s中共有%d个【%s】'%(file_name,number,old_word))
f.close()
print('您确定要把所有的【%s】替换为【%s】吗?'%(old_word,new_word))
YN = input('【YES/NO】:')
if YN == 'YES' or YN == 'yes':
f = open(file_name,'w')
for each_line in list1:
if old_word in each_line:
new_line = update(each_line.split(old_word),new_word = new_word)
line_index = list1.index(each_line)
list1.remove(each_line)
list1.insert(line_index,new_line)
f.writelines(list1)
f.close()