|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 zpqm1937 于 2020-3-12 23:59 编辑
- boy=[]
- girl=[]
- count = 1
- speak = open('record')
- for each_line in speak:
- role = each_line[0:3]
- line_spoken = each_line[4:]
- if a != '===':
- if role == '小甲鱼':
- boy.append(line_spoken)
- else:
- girl.append(line_spoken)
- else:
- file_boy_name = 'boy' + str(count) + '.txt'
- file_girl_name = 'girl' + str(count) + '.txt'
- boy_file = open(file_boy_name,'w')
- girl_file = open(file_girl_name,'w')
- boy_file.writelines(boy)
- girl_file.writelines(girl)
- boy_file.close()
- girl_file.close()
- boy=[]
- girl=[]
- count += 1
- speak.close()
复制代码
每次运行都报错,说我else 创建文件这段,缩进和空格使用不当,我也不清楚怎么回事。
本帖最后由 wuqramy 于 2020-3-13 10:08 编辑
你的else这段缩进错误,正确代码如下:
- boy=[]
- girl=[]
- count = 1
- speak = open('record.txt')
- for each_line in speak:
- role = each_line[0:3]
- line_spoken = each_line[4:]
- if role[0] != '==':
- if role == '小甲鱼':
- boy.append(line_spoken)
- else:
- girl.append(line_spoken)
- else:
- file_boy_name = 'boy' + str(count) + '.txt'
- file_girl_name = 'girl' + str(count) + '.txt'
- boy_file = open(file_boy_name,'w')
- girl_file = open(file_girl_name,'w')
- boy_file.writelines(boy)
- girl_file.writelines(girl)
- boy_file.close()
- girl_file.close()
- boy=[]
- girl=[]
- count += 1
- speak.close()
复制代码
|
|