|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目要求把‘“record.txt”的文件里的文本按照不同人分为boy_X.txt和girl_X.txt,但是答案的写法是已经知道只有“小甲鱼”和“小客服”两个人。
我想在这基础上,改成自动识别所有说话的人,并按照人的名字生成以人名开头的文件名,但我把下面代码运行后vs code没有告诉我哪里语句错了,就给了“NameError: name '小客服_1' is not defined”。
请大神帮忙看下我哪里错了
我的record.txt放在‘E:\a’文件夹里
- import os
- path = r'E:\a'
- os.chdir(path)
- text = open('record.txt')
- count = 1
- mans = []
- spokens = []
- for each_line in text:
- if each_line[:6] != '======':
- man,spoken = each_line.split(':',1)
- if man not in mans:
- mans.append(man)
- spokens.append([spoken])
- else:
- spokens[mans.index(man)].append(spoken)
- else:
- for i in range(len(mans)):
- exec('man%d_file_name = %s_%d.txt' %(i,mans[i],count))
- exec('man%d_file = open(man%d_file_name,%s)' %(i,i,'w'))
- exec('man%d_file.writelines(spokens[mans.index(man)])' %i)
- exec('man%d_file.close()' %i)
- mans = []
- spokens = []
- count += 1
-
- for i in range(len(mans)):
- exec('man%d_file_name = %s_%d.txt' %(i,mans[i],count))
- exec('man%d_file = open(man%d_file_name,%s)' %(i,i,'w'))
- exec('man%d_file.writelines(spokens[mans.index(man)])' %i)
- exec('man%d_file.close()' %i)
复制代码
思路应该没什么问题 不过emmm - exec('man%d_file_name = %s_%d.txt' %(i,mans[i],count))
- exec('man%d_file = open(man%d_file_name,%s)' %(i,i,'w'))
- exec('man%d_file.writelines(spokens[mans.index(man)])' %i)
- exec('man%d_file.close()' %i)
复制代码这几句可以这样用吗?没学过这种用法  ,感觉有语法错误
|
|