2

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)

2
  • 4
    As a general point, it's best not to name something str as that's the name of the built-in string type. Commented Oct 17, 2021 at 14:59
  • Refer to the link below for a guideline: stackoverflow.com/questions/1894269/… Commented Oct 17, 2021 at 15:02

6 Answers 6

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

If input has many spaces, then use re.split and ,\s* as separator
2

You could use this expression : str[1:-1].split(", ")

By the way it is not recommended to give a variable the name of a python type.

Comments

2

I suggest you use yaml:

import yaml
s = "[point, contextual, point]"
s = yaml.load(s, Loader=yaml.FullLoader)
print(s)

Output

['point', 'contextual', 'point']

Note:

yaml is a third party module to install it do (at the command line):

pip install pyyaml 

The yaml version of the code snippet above is 5.4.1

4 Comments

AttributeError: module 'yaml' has no attribute 'FullLoader'
Whats your version GhostOps?
mine is 3.13, and how do u update it? whats its name in pypi.org? sry cuz i haven't ever used yaml tho
@GhostOps Updated the answer, if you have already installed perhaps you have to use pip install --upgrade pyyaml
1

Because that's not a valid JSON string, the json.loads doesn't work. You need to do it manually and you can not use json module for the string you have.

st = "[point, contextual, point]"
lst = st[1:-1].split(', ')

On a side note, don't use str as a vairable: that's a builtin

Comments

1

Try this code:

string = "[point, contextual, point]"
print(string[1:-1].split(', '))

Outputs:

['point', 'contextual', 'point']

Tell me if its okay for you...

1 Comment

what is the point of the comprehension? split already returns a list
1

You can try this:

>>> st = "[point, contextual, point]"
>>> st[1:-1]
[point, contextual, point]

>>> st[1:-1].split(',')
['point', ' contextual', ' point'] # <- you have space

>>> list(map(lambda x: x.strip(), st[1:-1].split(',')))
['point', 'contextual', 'point']

why not split(', ')?

>>> st = "[point,   contextual,     point]"
>>> st[1:-1].split(', ') 
['point', '  contextual', '    point']

>>> list(map(lambda x: x.strip(), st[1:-1].split(',')))
['point', 'contextual', 'point']

1 Comment

Why not just split on ', ' instead?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.