lundi 12 juillet 2021

Python - Removing text after first instance of specific string and doing it again with a different string

I've got a text file of an export of some speech to text. It's meant to keep track of who's talking by placing a 1 or 2 at the beginning of the line. However it creates a new line depending on the services confidence.

I've been trying to use Python to remove the new line and join the instances of person 1 and 2 speaking. I'm fairly new to Python and my current best guess would be a series of if and else statements, but have had no luck.

The text currently appears as...

1 Hello and welcome

1 to this interview

1 Let's start by asking a few

1 questions

2 What questions

2 Do you have?

I'm trying to make it like...

1 Hello and welcome to this interview Let's start by asking a few questions

2 What questions Do you have?

My current code is this... Because everything else I've tried doesn't seem to work.

input = open("/mnt/d/Visual Studio Code/PythonStuff/TextReplace/SpeakerText.txt", "rt")

output = open("replacted_text.txt", "wt")

for line in input:

output.write(line.replace("1  ", "Speaker 1: ").replace("2    ", "Speaker 2: "))

input.close() output.close()

I had no luck finding a similar issue so any help would be appreciated, thank you!

Aucun commentaire:

Enregistrer un commentaire