将文件sketch.txt的Man和Other Man说的台词分开输出。Man的台词输出到man_data.txt 。Other Man 的台词输出到other_data.txt。

Man: Is this the right room for an argument?
Other Man: I've told you once.
Man: No you haven't!
Other Man: Yes I have.
Man: When?
Other Man: Just now.
Man: No you didn't!
Other Man: Yes I did!
Man: You didn't!
Other Man: I'm telling you, I did!
Man: You did not!
Other Man: Oh I'm sorry, is this a five minute argument, or the full half hour?
Man: Ah! (taking out his wallet and paying) Just the five minutes.
Other Man: Just the five minutes. Thank you.
Other Man: Anyway, I did.
Man: You most certainly did not!
Other Man: Now let's get one thing quite clear: I most definitely told you!
Man: Oh no you didn't!
Other Man: Oh yes I did!
Man: Oh no you didn't!
Other Man: Oh yes I did!
Man: Oh look, this isn't an argument!
(pause)
Other Man: Yes it is!
Man: No it isn't!
(pause)
Man: It's just contradiction!
Other Man: No it isn't!
Man: It IS!
Other Man: It is NOT!
Man: You just contradicted me!
Other Man: No I didn't!
Man: You DID!
Other Man: No no no!
Man: You did just then!
Other Man: Nonsense!
Man: (exasperated) Oh, this is futile!!
(pause)
Other Man: No it isn't!
Man: Yes it is!

先写了一个打印格式的模块nester.py:

import sys
def print_lol(the_list,indent=False,level=0,fn=sys.stdout):for each_item in the_list:if isinstance(each_item,list):print_lol(each_item,indent,level+1,fn)else:if indent:for tab_stop in ranage(level):print("\t",end='',file=fn)print(each_item,file=fn)

在nester目录下(该目录下须有nester.py和setup.py),
For Linux/Unix
执行这个命令:
python3 setup.py sdist
python 3setup.py install
(注意,若是已经装过nester模块,则需要修改setup.py的版本,再执行上面的两行命令。
For windows下:
C:\Users\ThinkPad\AppData\Local\Programs\Python\Python36-32\nester> C:\Users\ThinkPad\AppData\Local\Programs\Python\Python36-32**python.exe** setup.py sdist
C:\Users\ThinkPad\AppData\Local\Programs\Python\Python36-32**python.exe** setup.py install

再来写一个处理剧本台词的代码:

import nester
import os
import sysman=[ ]
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)elif role == 'Other Man':other.append(line_spoken)except ValueError:passdata.close()
except IOError:nester.print_lol('The data file is missing!')try:with open('man_data.txt','w') as man_file:nester.print_lol(man,fn=man_file)#将man保存到man_filewith open('other_data.txt','w') as other_file:nester.print_lol(other,fn=other_file)except IOError as err:nester.print_lol('File error:' + str(err))

最后执行:按F5.
可以在C:\Users\ThinkPad\AppData\Local\Progohtman_data.txt和other_data.txt。打开man_data.txt:

Is this the right room for an argument?
No you haven't!
When?
No you didn't!
You didn't!
You did not!
Ah! (taking out his wallet and paying) Just the five minutes.
You most certainly did not!
Oh no you didn't!
Oh no you didn't!
Oh look, this isn't an argument!
No it isn't!
It's just contradiction!
It IS!
You just contradicted me!
Youijust then!
(exasperated) Oh, this is futile!!
Yes it is!

python3处理剧本角色台词分开输出相关推荐

  1. 深度语义理解在剧本角色情感分析中的探索与应用

    深度语义理解在剧本角色情感分析中的探索与应用 1.背景 剧本分析是内容生产链条的第一环,因此,我们结合专家经验知识,大数据与自然语言处理计数来帮助业务部门快速分析和评估剧本.评估体系中涉及到了大量关于 ...

  2. 剧本角色情感分析赛后总结

    datafountain剧本角色情感分析赛后总结 模型的搭建 整数与浮点数提交之争 上下文的选择以及主语的加入 验证集和测试集的划分问题 多折模型训练及模型融合的方法 融合提升的要点 预训练操作 对抗 ...

  3. c语言输入字母输出数字,输入一字符串 把其中的字母和数字分开输出 用c语言编写...

    输入一字符串 把其中的字母和数字分开输出 用c语言编写 答案:6  信息版本:手机版 解决时间 2019-10-05 09:13 已解决 2019-10-04 22:32 输入一字符串 把其中的字母和 ...

  4. CCF BDCI 剧本角色情感识别:多目标学习开源方案

    1.赛题名称 剧本角色情感识别 比赛链接:https://www.datafountain.cn/competitions/518 2.赛题背景 剧本对影视行业的重要性不言而喻.一部好的剧本,不光是好 ...

  5. python3中文手册-Python 输入和输出

    Python 输入和输出 在前面几个章节中,我们其实已经接触了 Python 的输入输出的功能.本章节我们将具体介绍 Python 的输入输出. 输出格式美化 Python两种输出值的方式: 表达式语 ...

  6. Python3 装饰器进行log日志输出

    前言 使用Python 装饰器decorator来对函数进行日志输出_ 2021年3月 测试可用 环境 Python 3.7 代码 代码目录: logger.py -> 日志装饰器: 用来写lo ...

  7. SAP系统用户权限及岗位(复合)角色对应关系清单输出实例

    近期遇到一个由审计引出的需求,需要在系统中导出用户与其所分配的角色权限的对应关系.笔者公司建立了以岗位为管理单位的复合角色.需要定期输出并检查用户对应的岗位权限是否合理.比如,一个用户有总账会计角色权 ...

  8. Python3如何读取pdf文件并输出文本内容?

    学习python,不用再为pdf无法转换为word而烦恼.下面我们介绍python读取pdf文件(主要是针对文字部分). 安装需要的包: pip install pdfminer pip instal ...

  9. 吴裕雄--天生自然 PYTHON3开发学习:输入和输出

    str = input("请输入:"); print ("你输入的内容是: ", str) # 打开一个文件 f = open("/tmp/foo.t ...

最新文章

  1. tts代表_Text-to-Speech (TTS) Synthesis语音合成----控制语言合成
  2. java学习笔记15--多线程编程基础2
  3. php curl 传输大文件,空白目录 · php下载大文件curl · 看云
  4. python入门之函数结构函数的参数_python3基础之函数参数类型
  5. leetcode20. 有效的括号
  6. uva 11971 Polygon
  7. 关于使用MYSQL出现的内存泄漏问题
  8. catia如何将曲面加厚变为实体_?在CATIA中快速画一个大众车标
  9. python 手机号码归属地 软件_Python实现的手机号归属地相关信息查询功能示例
  10. java中args是什么意思?
  11. 面试官:知道你的接口QPS是多少么?
  12. c语言open()介绍
  13. 百度地图绘制行政区边界
  14. Qt之QML虚拟键盘
  15. Hive中的几种排序方式
  16. 复古风的小鱼缸,还有幻彩灯效加持,忆趣TV3.0体验
  17. 全景环视拼接,四路鱼眼摄像头,有个大概样子了
  18. InfiniBand网络简介
  19. python力扣刷题记录——204. 计数质数
  20. Mybatis基本使用步骤

热门文章

  1. 设计模式二、抽象工厂模式
  2. 做什么类型的产品最有成就感,最有趣?
  3. CentOS7安装gradle
  4. photoshop 快速画直线 1
  5. SpringBoot项目中计量单位与进制转换问题解决措施及数据校验怎么操作
  6. 乐于接受必然发生的情况,是克服随之而来的任何不幸的第一步!
  7. VPS2103 电流模式 PWM 控制器 4-50V IN /90V/0.1Ω功率管
  8. 架构设计:不要在一棵树上吊死
  9. 测试开发工程师必读经典好书清单,抽奖赠书送福利!
  10. 用Java写有关早上的语录,关于早上的励志语录