Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to list of strings in python

I have a string extracted from a .csv which has this format:

str = "[point, contextual, point]"

What I wanna do is convert it to a list in the format:

str = ["point", "contextual", "point"]

How can I do it? I tried with json.loads(str) but I got the error:

json.decoder.JSONDecodeError: Expecting value: line 1 column 2 (char 1)

like image 523
Fabio Avatar asked Dec 30 '25 16:12

Fabio


1 Answers

you could use:

my_str = "[point, contextual, point]"
my_str[1:-1].split(', ') # remove first and last characters ([]) and split on ", "

NB. don't use str as a variable name, this overwrites the str builtin

like image 76
mozway Avatar answered Jan 02 '26 07:01

mozway



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!