Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split string by comma so that each sentence will be a separate string

I want to split my string in separate sentences. My code is below

 import tensorflow as tf
 import pandas as pd 

 text = 'My cat is a great cat, this is very nice, you are doing great job'

 text = text.split(',')

Output:

 ['My cat is a great cat', ' this is very nice', ' you are doing great job']

But I want this output to be

 ['My cat is a great cat'] ['this is very nice'] ['you are doing great job']

is it possible??

like image 828
albert Avatar asked Mar 02 '26 18:03

albert


2 Answers

Yes it's possible, we have answers already given above but to match exact expected output as mentioned above use below:

inputlist = "apple,banana,cherry"
print(*[[s] for s in inputlist.split(',')],end=" ")

output:

['apple'] ['banana'] ['cherry'] 
like image 110
naveen p Avatar answered Mar 05 '26 06:03

naveen p


You just turn into a list of list with this

text = 'My cat is a great cat, this is very nice, you are doing great job'
text = [[x] for x in text.split(", ")]
like image 25
Hai Vu Avatar answered Mar 05 '26 08:03

Hai Vu