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('E:/test/dialogue.txt')
boy=[]
girl=[]
count=1
for each_line in f:
if each_line[:6] != '======':
(role,line_spoken) = each_line.split(':', 1) #每行按照:分割成1+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('E:/test/dialogue.txt')
E:\Python\python.exe "E:/PyCharm 2016.3.2/testest/abc/filelearn_01.py"
Process finished with exit code 0
http://edu.csdn.net/course/de... 是这个视频里的
已经成功了 搜索关键字发现在工作环境里
另外'e:\test/dialogue.txt' 当前者//\,后者可以//或\或/或;当前者/,后者也只能单斜。
有兴趣的可以试试 并tell me why