|
|
发表于 2020-7-14 11:41:43
|
显示全部楼层
本楼为最佳答案
重新改了下,现在可以生成三个文件了,之前只生成两个是因为 c = 2 在循环内了
- f = open(r'C:\Users\jiong\Desktop\record.txt')
- bfile = open('boy_1.txt', 'a')
- gfile = open('girl_1.txt', 'a')
- c = 2
- for each in f:
- filenameb = 'boy_' + str(c) + '.txt'
- filenameg = 'girl_' + str(c) + '.txt'
- if each[0:6] == '======':
- bfile.close() # 找到分隔符后,对之前的文件进行关闭保存
- gfile.close()
- bfile = open(filenameb, 'a')
- gfile = open(filenameg, 'a')
- c += 1
- else:
- (role, spoken) = each.split(':', 1)
- if role == '小甲鱼':
- bfile.write(spoken)
- if role == '小客服':
- gfile.write(spoken)
- bfile.close()
- gfile.close()
- f.close()
复制代码 |
|