猿问

python代码如下,报错,怎么解决?

f = open('E:\\11.11.txt')

boy = []
girl = []
count = 1

for each_line in f:
    if each_line in f[:6] != '======':
        (role,line_spoken) = each_line.split(':',1)
        if role == '小甲鱼':
            boy.append(line_spoken)
        if role == '小客服':
            girl.append(line_spoken)
    else:
        #文件分别保存操作#
        file_name_boy = 'boy_' + str(count) + '.txt'
        file_name_girl = 'boy_' + str(count) + '.txt'

        boy_file = open(file_name_boy,'w')
        girl_file = open(file_name_girl,'w')

        boy_file.writelines(boy)
        girl_file.writelines(girl)

        boy_file.close()
        girl_file.close()

        boy = []
        girl = []
        count += 1

f.close()

报错内容:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
  File "C:\Users\YIMUH\Desktop\11.26.py", line 8, in <module>
    if each_line in f[:6] != '======':
TypeError: '_io.TextIOWrapper' object is not subscriptable
>>>

涤非
浏览 2884回答 1
1回答

pardon110

报错信息都告诉你了 ,第8行错误,很明显的逻辑错误。if each_line in f[:6] != '======':\
随时随地看视频慕课网APP

相关分类

Python
我要回答