Reputation: 2687
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.
Upvotes: 0
Views: 199
Reputation: 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])
Upvotes: -1
Reputation: 473853
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']
Upvotes: 5
Reputation: 103814
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...
Upvotes: 1
Reputation: 365707
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?
Upvotes: 3