Reputation: 11
in Python v3.6: I have an entry coming in from database as a "single" string as follows:
'[snp500, vix]'
However I wish to read it as as a "list" of strings
['snp500', 'vix']
Can you please help how can I achieve this?
Upvotes: 1
Views: 35
Reputation: 5560
If you have
x = '[snp500, vix]'
You want to split around the [
brackets:
x = '[snp500, vix]'[1:-1].split(", ")
# ['np500', 'vi']
A safer method would be to use re
:
import re
re.split(",\s*", '[snp500, vix]'[1:-1])
This solution allows for 0 or more spaces after the comma.
Upvotes: 1