0

I would like to split a sting in python the most efficient way, and the most "python-like" way. Say I have this string:

s = '"Jens", "kasper", "Michael"' 

How do I achieve the following list:

names = ["David", "Kasper", "Michael"]

Meaning I would like to strip the names between the curly brackets.

3
  • 3
    I assume your list is meant to be Jens not David, right? Commented Oct 1, 2013 at 22:16
  • "strip the names between the curly brackets" - do you mean "extract the names between the quotes"? Commented Oct 1, 2013 at 22:17
  • How did you get that string in the first place? Commented Oct 1, 2013 at 22:19

4 Answers 4

5

Use ast.literal_eval():

Safely evaluate an expression node or a Unicode or Latin-1 encoded string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.

>>> from ast import literal_eval
>>> s = '"Jens", "kasper", "Michael"' 
>>> literal_eval(s)
('Jens', 'kasper', 'Michael')
>>> list(literal_eval(s))
['Jens', 'kasper', 'Michael']
Sign up to request clarification or add additional context in comments.

2 Comments

If the string is intended to be a Python tuple of strings representation, this is the right answer. But if literal_eval just happens to work on some format that wasn't intended to be a Python literal format, that usually isn't a good reason to use it.
@abarnert yeah, it looks like a good fit for the example that OP provided. But, I agree that the format is unusual (looks like a CSV with quoting) and we should know where did the OP get it. Thank you!
3

You can split it like this:

>>> s = '"Jens", "kasper", "Michael"' 
>>> s.split(', ')
['"Jens"', '"kasper"', '"Michael"']

You can strip the quotes like this:

>>> [name.strip('"') for name in s.split(', ')]
['Jens', 'kasper', 'Michael']

But really, you should consider how this weird string was constructed, and do the matching operation, instead of trying to build a parser from first principles. Are these Python literals? JSON strings? Something else? Were they joined together with ', '.join or the CSV module or something else?

1 Comment

Of course this doesn't magically capitalize kasper or convert Jens into David… if you actually want that, you'll need a fuller specification of what you're after.
1

Let's add a case to your string:

>>> s = '"Jens", "kasper", "Michael", "Jean Paul", "Bond, James"'
                                                        ^^       comma

You can use csv:

>>> import csv
>>> list(csv.reader([s], skipinitialspace=True))[0]
['Jens', 'kasper', 'Michael', 'Jean Paul', 'Bond, James']

Or a regex:

>>> import re
>>> [e.group(1) for e in re.finditer(r'"([^"]+)"',s)]
['Jens', 'kasper', 'Michael', 'Jean Paul', 'Bond, James']

The solution based on splitting on the comma will not work with the embedded comma:

>>> s = '"Jens", "kasper", "Michael"' 
>>> [e.strip().strip('"') for e in s.split(',')]
['Jens', 'kasper', 'Michael', 'Jean Paul', 'Bond', 'James']
                                               ^^^^  wrong answer...

Comments

-1

I think dawg has the best one, but:

stringthing = "Your string here!=-Hi!"
a = stringthing.split("=-")
print("Thing 1: " + a[0] + " Thing 2: " + a[1])

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.