任务:将文件(record.txt)中的数据进行分割并按照以下规律保存起来:
--小甲鱼的对话单独保存为boy_*.txt的文件(去掉“小甲鱼:”)
--小客服的对话单独保存为girl_*.txt的文件(去掉“小客服:”)
--文件中总共有三段对话,分别保存为boy_1.txt,girl_1.txt,boy_2.txt,girl_2.txt,boy_3.txt,girl_3.txt共6个文件
(提示:文件中的不同对话见已经使用“=========”分割)
def split_file(boy,girl,count):
boy_file_name = 'boy_' + str(count) + '.txt'
girl_file_name = 'girl_' + str(count) + '.txt'
boy_file = open(boy_file_name,'w')
girl_file = open(girl_file_name,'w')
boy_file.writelines(boy)
girl_file.writelines(girl)
boy_file.close()
girl_file.close()
def cut_file(file):
f = open('C:/record.txt')
boy = []
girl = []
count = 1
for each_line in f:
if each_line[:5] != '=====':
(role,line_spoken) = each_line.split(':',1)
if role == '小甲鱼':
boy.append(line_spoken)
if role == '小客服':
girl.append(line_spoken)
else:
split_file(boy,girl,count)
boy = []
girl = []
count += 1
split_file(boy,girl,count)
f.close()
cut_file('C:/record.txt')
------------------------分割线,哈哈哈---------------------------
动动手:
0.编写一个程序,接受用户的输入并保存为新的文件:
先输入文件名,然后打开文件,调用write将输入的字符串写入文件中,以:w作为写完标识符,写完之后调用close函数即完成文件的写入。
file_name = input('请输入文件名:')
print('请输入内容[单独输入":w"保存退出]:')
f = open(file_name,'w')
while 1:
text = input()
if text == ':w':
break
else:
f.write('%s\n'%text)
f.close()
使用函数:
def file_writen(file_name):
f = open(file_name,'w')
while 1:
text = input()
if text == ':w':
break
else:
f.write('%s\n'%text)
f.close()
file_name = input('请输入文件名:')
print('请输入内容[单独输入":w"保存退出]:')
file_writen(file_name)
编写一个程序比较用户输入的两个文件,如果不同,显示出所有不同处的行号与第一个不同字符的位置:
def compare(file1,file2):
f1 = open(file1)
f2 = open(file2)
count = 0
diff = []
for line1 in f1:
line2 = f2.readline()
count +=1
if line1 != line2:
diff.append(count)
f1.close()
f2.close()
return diff
file1 = input('请输入第一个文件名:')
file2 = input('请输入第二个文件名:')
diff = compare(file1,file2)
length = len(diff)
if length == 0:
print('两个文件完全相同。')
else:
print('两个文件共有%d处不同:'%length)
for i in diff:
print('第%d行不一样'%i)
2.编写一个程序,当用户输入文件名和行数(N)后,将该文件的前N行内容打印到屏幕上:
def readfile(filename,linenum):
f = open(filename)
for i in range(N):
print(f.readline(),end = '')
f.close()
filename = input('请输入要打开的文件(C:\\test.txt):')
N = int(input('请输入要显示的前几行行数:'))
readfile(filename,N)
3.呃,不得不说我们的用户越来越刁钻了。要求在上一题的基础上扩展,用户可以随意输入需要显示的行数。(如输入13:21打印第13行到第21行,输入:21打印前21行,输入21:则打印从第21行开始到文件结尾所有内容)
def readfile(filename,linenum):
(fronn,backk) = linenum.split(':')
f = open(filename)
if fronn == '' or fronn == '0':
fron = 1
fron = int(fron)
back = int(backk)
for j in range(fron,back+1):
print(f.readline(),end = '')
if backk == '':
fron = int(fronn)
for i in range(fron-1):
f.readline()
for each_line in f:
print(each_line,end='')
if fronn != '' and backk != '':
fron = int(fronn)
back = int(backk)
for i in range(fron-1):
f.readline()
for j in range(fron,back+1):
print(f.readline(),end = '')
f.close()
filename = input('请输入要打开的文件(C:\\test.txt):')
N = input('请输入要显示的行数[格式如13:21或:21或21:]:')
readfile(filename,N)
稍微改动了点的:
def read_file(filename,lines):
fron = 0
back = 0
fronn = ''
backk = ''
f = open(file,encoding='UTF-8')
if lines == '':
fronn = ''
backk = ''
else:
(fronn,backk) = lines.split(':')
if fronn == '' and backk == '':
print('文件%s的全部内容如下:'%file)
for each_line in f:
print(each_line,end='')
print('\n')
if fronn == '' and backk != '':
back = int(backk)
print('文件%s从开始到第%d行的内容如下:'%(file,back))
for i in range(back):
print(f.readline(),end='')
print('\n')
if fronn != '' and backk == '':
fron = int(fronn)
for i in range(fron-1):
f.readline()
print('文件%s从第%d行开始到末尾的内容如下:'%(file,fron))
for each_line in f:
print(each_line,end='')
print('\n')
f.close()
while 1:
file = input('请输入要打开的文件(C:\\test.txt)或者【输入Q或q退出】:')
if file == 'Q' or file == 'q':
break
lines = input('请输入需要显示的行数【格式如13:21或:21或21:】:')
read_file(file,lines)
4.编写一个程序,实现“全部替换”功能
def char_replace(filename,old,new):
newline = []
count = 0
f = open(filename)
for each_line in f:
if old in each_line:
count += 1
each_line = each_line.replace(old,new)
newline.append(each_line)
ok = input('文件%s中共有%d个【%s】\n您确定要把所有的【%s】替换为【%s】吗?\n【YES/NO】:'%(filename,count,old,old,new))
if ok == 'YES' or ok == 'yes' or ok == 'Yes':
fnew = open(filename,'w')
fnew.writelines(newline)
fnew.close()
f.close()
filename = input('请输入文件名:')
old = input('请输入需要替换的单词或字符:')
new = input('请输入新的单词或字符:')
char_replace(filename,old,new)