|
|

楼主 |
发表于 2017-3-26 15:25:09
|
显示全部楼层
- 受访者说 = [] #定义三个空列表
- 记者问 = []
- 非记者问 = []
- try: #使用try-except语句处理异常
- with open ('news_release_interview_chinese.txt', encoding='utf8') as data: #使用with语句来处理文件
- for each_line in data: #for循环
- try:
- (timecode, role, line_spoken) = each_line.split(':', 2) #对文档中的 : 进行分隔,并分别给三段命名
- line_spoken = line_spoken.strip() #利用strip方法去除空白符(参数为空时默认删除空白符)
- if role == '甘绍宁': #条件语句筛选
- 受访者说.append(line_spoken)
- else:
- if '记者' in role:
- 记者问.append(line_spoken)
- else:
- 非记者问.append(line_spoken)
- except ValueError:
- pass
- except IOError as reason:
- print("想打开的文件不存在,请检测文件目录!" + str(reason))
- try: #把列表写入文件,同样用with语句
- with open('output_interviewee.txt', 'w', encoding='utf8') as interviewee_file:
- print (受访者说, file = interviewee_file)
- with open('output_journalists.txt', 'w', encoding='utf8') as journalists_file:
- print (记者问, file = journalists_file)
- with open('output_journalists_not.txt', 'w', encoding='utf8') as journalists_not_file:
- print (非记者问, file = journalists_not_file)
- except IOError as reason:
- print ('文件处理发生问题!' + str(reason) )
- output_lines = {"受访者说":受访者说, "记者问":记者问, "非记者问":非记者问}
- import pickle #导入模块
- import json
- try:
- with open('output_lines.pickle', 'wb') as output_file:
- pickle.dump(output_lines, output_file)
- with open('output_lines.json', 'wb') as output_file:
- json.dump(output_lines, output_file)
- except IOError as err:
- print ('文件处理发生问题!' + str(err) )
- except pickle.PickleError as perr:
- print ('Pickling error:' + str(perr))
复制代码 |
|