鱼C论坛

 找回密码
 立即注册
查看: 2484|回复: 16

[已解决]一个关于条件语句的问题

[复制链接]
发表于 2017-3-25 22:43:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 观测者 于 2017-3-26 12:55 编辑

  1. interviewee = []
  2. journalists = []
  3. journalists_not = []
  4. try:
  5.     with open ('news_release_interview_chinese.txt', encoding='utf8') as date:
  6.         for each_line in date:
  7.             try:
  8.                 (timecode, role, line_spoken) = each_line.split(':', 2)
  9.                 line_spoken = line_spoken.strip()
  10.                 if role == '甘绍宁':
  11.                     interviewee.append(line_spoken)
  12.                 else:
  13.                     if '记者' in role:
  14.                         journalists.append(line_spoken)
  15.                     else:
  16.                         journalists_not.append(line_spoken)
  17.             except ValueError:
  18.                 pass
  19.             
  20. except IOError as reason:
  21.        print('想打开的文件不存在,请检查文件目录' + str(reason))
  22. try:
  23.     with open ('output_interviewee.txt', 'w', encoding='utf8') as interviewee_file,open ('output_journalists.txt', 'w', encoding='utf8') as journalists_file,open('output_journalists_not.txt', 'w', encoding='utf8') as journalists_not_file:
  24.         print(interviewee, file = interviewee_file)
  25.         print(journalists, file = journalists_file)
  26.         print(journalists_not, file = journalists_not_file)
  27. except IOError as err:
  28.     print ('文件處理發生問題!' + str(err) )
复制代码


问题是在line14中的if条件语句中的role'记者'在文档中有很多类别如中间电视台记者、凤凰电视台记者等,我该如何修改这段代码?
我这段代码报错如图所示
以及为何输出的文件里为空列表??文档见下楼

最佳答案
2017-3-26 15:38:26

42行
with open('output_lines.json', 'w') as output_file:
QQ浏览器截屏未命名.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-3-25 22:49:52 | 显示全部楼层
用了中文冒号!
  1. 受访者说 = []
  2. 记者问 = []
  3. 非记者问 = []
  4. try:
  5.     with open ('news_release_interview_chinese.txt', encoding='utf8') as date:
  6.         for each_line in date:
  7.             try:
  8.                 (timecode, role, line_spoken) = each_line.split(':', 2)
  9.                 line_spoken = line_spoken.strip()
  10.                 if role == '甘绍宁':
  11.                     受访者说.append(line_spoken)
  12.                 else:
  13.                     if role == '记者':
  14.                         记者问.append(line_spoken)
  15.                     else:
  16.                         非记者问.append(line_spoken)
  17.             except ValueError:
  18.                 pass
  19. except IOError as reason:
  20.     print('想打开的文件不存在,请检查文件目录' + str(reason))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2017-3-25 23:06:50 | 显示全部楼层
  1. 受访者说 = []
  2. 记者问 = []
  3. 非记者问 = []
  4. try:
  5.     with open ('news_release_interview_chinese.txt', encoding='utf8') as date:
  6.         for each_line in date:
  7.             try:
  8.                 (timecode, role, line_spoken) = each_line.split(':', 2)
  9.                 line_spoken = line_spoken.strip()
  10.                 if role == '甘绍宁':
  11.                     受访者说.append(line_spoken)
  12.                 else:
  13.                     if role == '记者':
  14.                         记者问.append(line_spoken)
  15.                     else:
  16.                         非记者问.append(line_spoken)
  17.             except ValueError:
  18.                 pass
  19. except IOError as reason:
  20.     print('想打开的文件不存在,请检查文件目录' + str(reason))
  21. try:
  22.     with open ('output_interviewee.txt', 'w', encoding='utf8') as interviewee_file:
  23.         print(受访者说, file = interviewee_file)
  24.     with open ('output_journalists.txt', 'w', encoding='utf8') as journalists_file:
  25.         print(记者问, file = journalists_file)
  26.     with open ('output_journalists_not.txt', 'w', encoding='utf8') as journalists_not_file:
  27.         print(非记者问, file = journalists_not_file)
  28. except IOError:
  29.     print('文件处理发生问题!')
复制代码


谢谢,我还想问问为什么输出的文件都是空列表
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-25 23:09:15 | 显示全部楼层
问题是在line13中的if条件语句中的role'记者'在文档中有很多类别如中间电视台记者、凤凰电视台记者等,我该如何修改这段代码?
可以改为:
if '记者' in role:
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-3-25 23:26:15 | 显示全部楼层
冬雪雪冬 发表于 2017-3-25 23:09
问题是在line13中的if条件语句中的role'记者'在文档中有很多类别如中间电视台记者、凤凰电视台记者等,我该 ...

谢谢,请问为什么我输出之后的文件都是空列表
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-25 23:39:45 | 显示全部楼层
观测者 发表于 2017-3-25 23:26
谢谢,请问为什么我输出之后的文件都是空列表

你把txt文件放上来我看看。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-3-26 00:01:56 | 显示全部楼层

文件

本帖最后由 观测者 于 2017-3-26 08:32 编辑

文件

news_release_interview_chinese.txt

24.72 KB, 下载次数: 2

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-3-26 00:02:38 | 显示全部楼层
冬雪雪冬 发表于 2017-3-25 23:09
问题是在line13中的if条件语句中的role'记者'在文档中有很多类别如中间电视台记者、凤凰电视台记者等,我该 ...
  1. 受访者说 = []
  2. 记者问 = []
  3. 非记者问 = []
  4. try:
  5.     with open ('news_release_interview_chinese.txt', encoding='utf8') as date:
  6.         for each_line in date:
  7.             try:
  8.                 (timecode, role, line_spoken) = each_line.split(':', 2)
  9.                 line_spoken = line_spoken.strip()
  10.                 if role == '甘绍宁':
  11.                     受访者说.append(line_spoken)
  12.                 else:
  13.                     if role == '记者':
  14.                         记者问.append(line_spoken)
  15.                     else:
  16.                         非记者问.append(line_spoken)
  17.             except ValueError:
  18.                 pass
  19. except IOError as reason:
  20.     print('想打开的文件不存在,请检查文件目录' + str(reason))
  21. try:
  22.     with open ('output_interviewee.txt', 'w', encoding='utf8') as interviewee_file:
  23.         print(受访者说, file = interviewee_file)
  24.     with open ('output_journalists.txt', 'w', encoding='utf8') as journalists_file:
  25.         print(记者问, file = journalists_file)
  26.     with open ('output_journalists_not.txt', 'w', encoding='utf8') as journalists_not_file:
  27.         print(非记者问, file = journalists_not_file)
  28. except IOError:
  29.     print('文件处理发生问题!')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-26 00:35:23 | 显示全部楼层
你的意思是不是要把所有包含“记者”两个字的都算是记者?
如果这样的话需要用正则表达式
其他没注意看,你最好用代码格式发出来吧,不然看都不想看你的代码,没一点层次感,累
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-26 00:38:44 | 显示全部楼层
观测者 发表于 2017-3-25 23:06
谢谢,我还想问问为什么输出的文件都是空列表

我觉得变量最好不要用中文吧。用英文作为变量会比较好
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-3-26 08:05:54 | 显示全部楼层
文件在此

news_release_interview_chinese.txt

24.72 KB, 下载次数: 0

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-3-26 08:11:19 | 显示全部楼层
gopythoner 发表于 2017-3-26 00:35
你的意思是不是要把所有包含“记者”两个字的都算是记者?
如果这样的话需要用正则表达式
其他没注意看, ...
  1. interviewee = []
  2. journalists = []
  3. journalists_not = []
  4. try:
  5.     with open ('news_release_interview_chinese.txt', encoding='utf8') as date:
  6.         for each_line in date:
  7.             try:
  8.                 (timecode, role, line_spoken) = each_line.split(':', 2)
  9.                 line_spoken = line_spoken.strip()
  10.                 if role == '甘绍宁':
  11.                     interviewee.append(line_spoken)
  12.                 else:
  13.                     if '记者' in role:
  14.                         journalists.append(line_spoken)
  15.                     else:
  16.                         journalists_not.append(line_spoken)
  17.             except ValueError:
  18.                 pass
  19.             
  20. except IOError as reason:
  21.        print('想打开的文件不存在,请检查文件目录' + str(reason))
  22. try:
  23.     with open ('output_interviewee.txt', 'w', encoding='utf8') as interviewee_file,open ('output_journalists.txt', 'w', encoding='utf8') as journalists_file,open('output_journalists_not.txt', 'w', encoding='utf8') as journalists_not_file:
  24.         print(interviewee, file = interviewee_file)
  25.         print(journalists, file = journalists_file)
  26.         print(journalists_not, file = journalists_not_file)
  27. except IOError as err:
  28.     print ('文件處理發生問題!' + str(err) )
复制代码


输出的是空列表

news_release_interview_chinese.txt

24.72 KB, 下载次数: 1

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-3-26 09:27:51 From FishC Mobile | 显示全部楼层
求助
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-3-26 12:56:27 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-3-26 13:23:31 | 显示全部楼层
12楼的第8行,冒号从txt文档里面复制,那个是全角的:
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-3-26 15:25:09 | 显示全部楼层
ooxx7788 发表于 2017-3-26 13:23
12楼的第8行,冒号从txt文档里面复制,那个是全角的:

  1. 受访者说 = []               #定义三个空列表
  2. 记者问 = []
  3. 非记者问 = []

  4. try:        #使用try-except语句处理异常
  5.     with open ('news_release_interview_chinese.txt', encoding='utf8') as data: #使用with语句来处理文件
  6.         for each_line in data:      #for循环
  7.             try:
  8.                 (timecode, role, line_spoken) = each_line.split(':', 2)      #对文档中的 : 进行分隔,并分别给三段命名
  9.                 line_spoken = line_spoken.strip()    #利用strip方法去除空白符(参数为空时默认删除空白符)
  10.                 if role == '甘绍宁':       #条件语句筛选
  11.                     受访者说.append(line_spoken)
  12.                 else:
  13.                     if '记者' in role:
  14.                         记者问.append(line_spoken)
  15.                     else:
  16.                         非记者问.append(line_spoken)
  17.             except ValueError:
  18.                 pass
  19. except IOError as reason:
  20.     print("想打开的文件不存在,请检测文件目录!" + str(reason))

  21. try:        #把列表写入文件,同样用with语句
  22.     with open('output_interviewee.txt', 'w', encoding='utf8') as interviewee_file:
  23.         print (受访者说, file = interviewee_file)
  24.     with open('output_journalists.txt', 'w', encoding='utf8') as journalists_file:
  25.         print (记者问, file = journalists_file)
  26.     with open('output_journalists_not.txt', 'w', encoding='utf8') as journalists_not_file:
  27.         print (非记者问, file =  journalists_not_file)
  28. except IOError as reason:
  29.     print ('文件处理发生问题!' + str(reason) )

  30. output_lines = {"受访者说":受访者说, "记者问":记者问, "非记者问":非记者问}

  31. import pickle            #导入模块
  32. import json

  33. try:
  34.     with open('output_lines.pickle', 'wb') as output_file:
  35.         pickle.dump(output_lines, output_file)
  36.     with open('output_lines.json', 'wb') as output_file:
  37.         json.dump(output_lines, output_file)
  38. except IOError as err:
  39.     print ('文件处理发生问题!' + str(err) )
  40. except pickle.PickleError as perr:
  41.     print ('Pickling error:' + str(perr))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-26 15:38:26 | 显示全部楼层    本楼为最佳答案   

42行
with open('output_lines.json', 'w') as output_file:
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-26 09:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表