1

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?

1
  • Which database and connector package are you using? And what is the type of the column in the database? Commented Jun 26, 2021 at 17:23

1 Answer 1

1

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.

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

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.