引入列表,将对话分类添加到对应列表中

man = []        #创建列表
other = []
try:
    data = open('sketch.txt')

    for each_line in data:
        try:
            (role, line_spoken) = each_line.split(':', 1)
            line_spoken = line_spoken.strip()   #去除空白符
            if role == 'Man':
               man.append(line_spoken)      #将man的话添到man[]
               
            elif role == 'Other Man':       
               other.append(line_spoken)        #将other man的话添到列表
           
             

       
        except ValueError:      #异常处理
            pass
    if 'data' in locals():      #调用方法locals()判断‘data’文件是否存在当前环境中
        data.close()
    
except IOError:     #I/O错误处理
    print('The data file is missing!')

print(man)
print(other)
    

 

posted @ 2018-08-23 19:32  nester_liz  阅读(140)  评论(0)    收藏  举报