|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
自己练习小甲鱼课上的代码,给定一个聊天文件,截取不同角色的聊天内容分别存放。我在读文件的时候明明用的是writelines方法,参数也是list类型,编译总通不过,还说write方法参数应该是一个字符串。具体报错如下(没有权限发图片,暂且发文字):
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-147-7230df073f84> in <module>()
19 with open(f_file,'w') as f, open(m_file,'w') as m:
20 print('boy的类型是%s' % type(boy))
---> 21 f.writelines(boy)
22 m.writelines(girl)
23 boy.clear()
TypeError: write() argument must be str, not list
以下是代码:
- boy=[]
- girl=[]
- i=1
- count=1
- with open('all.txt','r') as all :
- for line in all:
- if line[0] != '=':
- (role, line_spoken)=line.split(':',1)
- if role[1] == '男':
- boy.append([line_spoken+str(count)])
- count+=1
- else:
- girl.append([line_spoken+str(count)])
- count += 1
- else:
- f_file='f'+str(i)+'.txt'
- m_file='m'+str(i)+'.txt'
- with open(f_file,'w') as f, open(m_file,'w') as m:
- print('boy的类型是%s' % type(boy))
- f.writelines(boy)
- m.writelines(girl)
- boy.clear()
- girl.clear()
- i+=1
-
- f_file='f'+str(i)+'.txt'
- m_file='m'+str(i)+'.txt'
- with open(f_file,'w') as f, open(m_file,'w') as m:
- f.writelines(boy)
- m.writelines(girl)
- boy.clear()
- girl.clear()
复制代码 |
|