3

I have a string field : "[Paris, Marseille, Pays-Bas]". I want to convert this string to a list of strings.

For now I have the following function :

def stringToList(string):
    string = string[1:len(string)-1]
    try:
        if len(string) != 0: 
            tempList = string.split(", ")
            newList = list(map(lambda x: str(x), tempList))
        else:
            newList = []
    except:
        newList = [-9999]

    return(newList)

I want to know if there is a simpler or a shorter method with the same results. I could use ast.literal_eval() if my input data were of type int. But in my case, it does not work.

Thank you

4 Answers 4

4

Worth to know:

import re

string = "[Paris, Marseille, Pays-Bas]"
founds = re.findall('[\-A-Za-z]+', string)

It will find all that consist at least one of of -, A-Z, and a-z.

One pros is that it can work with less-neat strings like:

string2 = " [  Paris, Marseille  , Pays-Bas  ] "
string3 = "   [ Paris  ,  Marseille  ,   Pays-Bas  ] "
Sign up to request clarification or add additional context in comments.

2 Comments

this is my favorite option but OP must somehow be certain of what can appear between the commas. For example as this answer now stands "[Paris1, M4rseill3]" would not work. It might therefore not be a bad idea to add a \d to the mix.
@Ev.Kounis You're right. Additionally, I personally believe char-by-char processing is the most proper approach for this kind of parsing problem, which is exactly what re internally does. Stripping left and right sides, splitting, and conditionally selecting elements are can be simply done in code-level, however, internally it is a bit painful... I think...
3

This splits it into a list of strings:

'[Paris, Marseille, Pays-Bas]'.strip('[]').split(', ')                                                                                                                               
# ['Paris', 'Marseille', 'Pays-Bas']

2 Comments

Isn't this what i did?
Yes, it looks like we both did at about the same time
2

Just use slicing and str.split:

>>> s = '[Paris, Marseille, Pays-Bas]'
>>> s[1:-1].split(', ')
['Paris', 'Marseille', 'Pays-Bas']
>>> 

Or str.strip with str.split:

>>> s = '[Paris, Marseille, Pays-Bas]'
>>> s.strip('[]').split(', ')
['Paris', 'Marseille', 'Pays-Bas']
>>> 

Comments

0

try this :

s = "[Paris, Marseille, Pays-Bas]"

s = [i.replace('[','').replace(']','').replace(' ','') for i in 
s.split(',')]
print(s)

output:

['Paris', 'Marseille', 'Pays-Bas']

Comments

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.