在Python中拆分文本

myn*_*ame 2 python text split

每次出现特定类型的字体时,是否有一种简单的方法将文本拆分为单独的行.例如,我的文字看起来像这样:

BILLY: The sky is blue. SALLY: It really is blue. SAM: I think it looks like this: terrible.
Run Code Online (Sandbox Code Playgroud)

我想将文本分成每个发言者的行:

BILLY: The sky is blue.
SALLY: It really is blue.
SAM: I think it looks like this: terrible.
Run Code Online (Sandbox Code Playgroud)

扬声器总是大写,名字后跟冒号.

vks*_*vks 11

import re
a="BILLY: The sky is blue. SALLY: It really is blue. SAM: I think it looks like this: terrible."
print re.split(r"\s(?=[A-Z]+:)",a)
Run Code Online (Sandbox Code Playgroud)

你可以用re.split它.

输出:['BILLY: The sky is blue.', 'SALLY: It really is blue.', 'SAM: I think it looks like this: terrible.']