Home > Enterprise >  How to remove one colon and text between two colons in python?
How to remove one colon and text between two colons in python?

Time:10-05

This is my input text is :

[2.29 5.94] Person_1 : Neutral : thank you for calling how can i help you today
[13.24 13.96] Person_1 : okay 
[16.72 20.4] Person_1 : sad : oh okay 
[6.21 10.65] Person_2 : hi  this is XYZ
[13.12 16.99] Person_2 : its : actually its good

Here, if two colons are there then I need to remove one colon and text between two colons.Text between two colons can be anything.

Expected is :

[2.29 5.94] Person_1 : thank you for calling how can i help you today
[13.24 13.96] Person_1 : okay 
[16.72 20.4] Person_1 : oh okay 
[6.21 10.65] Person_2 : hi  this is XYZ
[13.12 16.99] Person_2  : actually its good

CodePudding user response:

line = "[2.29 5.94] Person_1 : Neutral : thank you for calling how can i help you today"
content = line.split(":")
new_line = ":".join([content[0], content[-1]])

# Output new_line: '[2.29 5.94] Person_1 : thank you for calling how can i help you today'

CodePudding user response:

You can use regex and re.sub like below: ( with :.*: you find everything between :...: then replace with only one :)

import re

txts = ['[2.29 5.94] Person_1 : Neutral : thank you for calling how can i help you today','[13.24 13.96] Person_1 : okay ','[16.72 20.4] Person_1 : sad : oh okay ','[6.21 10.65] Person_2 : hi  this is XYZ','[13.12 16.99] Person_2 : its : actually its good']

out = [re.sub(r':.*:',':',txt) for txt in txts]

Output:

>>> out
['[2.29 5.94] Person_1 : thank you for calling how can i help you today',
 '[13.24 13.96] Person_1 : okay ',
 '[16.72 20.4] Person_1 : oh okay ',
 '[6.21 10.65] Person_2 : hi  this is XYZ',
 '[13.12 16.99] Person_2 : actually its good']
  • Related