我有一个电影剧本《逝者》,我想通过角色的名字来解析数据。文本文件的格式没有分隔符,但是它的字符名都是BILLY。我唯一的标识符是所有大写字母的名字。我通读了regex和其他线程,但我不确定从哪里开始。。。。。在
file = open("Departed.txt","r")
data = file.read()
pattern = re.compile(r'BILLY')
matches = pattern.finditer(data)
for match in matches:
print(match)
这仍然返回整个脚本。。。 https://pastebin.com/226VzLWu
这里有一个快速完成的方法(您仍然需要对此进行清理,但我认为您的答案在这里):
这将为您提供:
^{pr2}$Python已经在regex模块中内置了split,所以请尝试:
我的输出基于您的评论(我使用的是Python3):
结果列表:
['Not sure if this helps, but this is some sample text.', 'YOUNG', 'COLIN Yeah.', "COSTELLO tells the Proprietor to takes three loaves of bread and some soup off the shelves and puts them in Colin's bag.", 'COSTELLO Get him three loaves of bread. And a couple of half gallons of milk. And some soup. He goes over to the fridge and puts two half gallons of milk in the bag. Some soup. Costello turns to Colin.']
格式有点混乱,但是仅仅依赖大写会导致一些问题,因为当脚本中有另一个字符提到这些名称时,这些名称都是大写的。我发现的最好的结果似乎是将8个空格转换成制表符,然后在每个制表符组后面拆分行。结果如下:
对于这种转变,这里有一句话:
^{pr2}$然后您可以看到一些格式,并可能区分行和方向。此脚本尝试猜测内容类型(尽管稍后确实有点困难)
输出:
相关问题 更多 >
编程相关推荐