需要重新格式化文本文件,将演讲者文本向上移动一行到演讲者标签

我有许多包含需要重新格式化的文本的 .txt 文件。具体来说,我有 Speaker A 和 Speaker B,文本在下一行。


A:

I can not believe the weather today .

B:

It is beautiful outside .

A:

Really nice .

B:

Okay , how are you doing ?

A:

I am good .

B:

Good to hear .

A:

Thank you .

可以有更多的发言者,但所有人都会在他们的标签前加上 : 。


我希望文件输出为:


A: I can not believe the weather today .

B: It is beautiful outside .

A: Really nice .

B: Okay , how are you doing ?

A: I am good .

B: Good to hear .

A: Thank you .

谢谢。


编辑:


另外,如果说话者标签之间有多行文本,是否有解决方案?例如:


A:

Well hello . 

Long time no see . 

How are you doing ? 

B:

Good . 

How are you ?

A:

Really great .

B:

Good .

有了预期的结果...


A: Well hello . Long time no see . How are you doing ? 

B: Good . How are you ?

A: Really great .

B: Good .


凤凰求蛊
浏览 120回答 2
2回答

GCT1015

正则表达式替换可以处理这个:import retext = """A:I can not believe the weather today .B:It is beautiful outside ."""text = re.sub(r"^(\w+:)\s*", r"\1 ", text, flags=re.MULTILINE)print(text)# A: I can not believe the weather today .# B: It is beautiful outside .编辑:基于更新的问题,对于多线对话:import retext = """A:Well hello . Long time no see . How are you doing ? B:Good . How are you ?"""text = re.sub(r"(.*?)\s*\n(?!\w+:)", r"\1 ", text, flags=re.MULTILINE)print(text)# A: Well hello . Long time no see . How are you doing ?# B: Good . How are you ?

繁花不似锦

如果短语在一行上,这应该有效:lines = file.readlines()for ii in range(1,len(lines),2):    print(lines[ii-1][:-1]+lines[ii])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python