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

Viewed 37

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!

2 Answers

We can use a regex iteration approach here. Parse the input text, and then build out the numbered questions, which are then stored in a dictionary.

inp = """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?"""

num = "1"
questions = {}

for m in re.finditer(r'^(\d+) (.*)$', inp, flags=re.MULTILINE):
    num_curr = m.group(1)
    if num_curr not in questions:
        questions[num_curr] = m.group(2)
        num = num_curr
    else:
        questions[num_curr] = questions[num_curr] + ' ' + m.group(2)

print(questions)

This prints:

{'1': "Hello and welcome to this interview Let's start by asking a few questions",
 '2': 'What questions Do you have?'}

One way using collections.defaultdict:

from collections import defaultdict

d = defaultdict(list)

for line in input:
    num, txt = line.split(maxsplit=1)
    d[num].append(txt)
for k, v in d.items():
    print(k, " ".join(v))

Output:

1 Hello and welcome to this interview Let's start by asking a few questions
2 What questions Do you have?
Related