说明:
1.把冒号和前面的名字去掉
2.男的台词放一个txt文件,女的台词放一个txt文件
3.遇到======就重新生成一个文件
record.txt:
1 婷婷:迪迪早啊! 2 迪迪:早啊! 3 婷婷:111111111111 4 迪迪:22222222222 5 婷婷:1111111111111 6 迪迪:2222222222 7 婷婷:1111111111 8 迪迪:2222222222 9 婷婷:111111111111 10 迪迪:2222222222 11 婷婷:11111111111 12 迪迪:2222222222 13 婷婷:111111111 14 迪迪:222222222 15 ====================================== 16 婷婷:3333333333 17 迪迪:4444444 18 婷婷:333333333 19 迪迪:444444444444 20 婷婷:33333333333 21 迪迪:4444444444 22 婷婷:333333333 23 迪迪:444444444444 24 婷婷:333333333 25 迪迪:4444444 26 ====================================== 27 婷婷:55 28 迪迪:66666666666 29 婷婷:5555555555 30 迪迪:6666 31 婷婷:5555555555
test.py:
1 f = open("e:/record.txt") 2 3 def chat(): 4 listGirl = [] 5 listBoy = [] 6 #段的计数器 7 i = 0 8 #遍历record每一行 9 for eachline in f: 10 if "婷婷:" in eachline: 11 (girlName,girlWords) = eachline.split(":") 12 listGirl.append(girlWords) 13 elif "迪迪:" in eachline: 14 (boyname,boyWords) = eachline.split(":") 15 listBoy.append(boyWords) 16 elif "=" in eachline: 17 #创建两个txt文件 18 girl = open("e:/girl_"+str(i+1)+".txt","w") 19 boy = open("e:/boy_"+str(i+1)+".txt","w") 20 girl.writelines(listGirl) 21 boy.writelines(listBoy) 22 # 初始化数组 23 listGirl = [] 24 listBoy = [] 25 i += 1 26 girl = open("e:/girl_"+str(i+1)+".txt","w") 27 boy = open("e:/boy_"+str(i+1)+".txt","w") 28 girl.writelines(listGirl) 29 boy.writelines(listBoy) 30 chat()
最后生成: