1

I need to convert string like below to list using Python.

sample_str = '["sample text1", "\'sample text2\'", "sample text3"]'

If we check the data type of "sample_str" above, it will be string. I need to know if there a way I could make it a list like below :

sample_str_to_list = ["sample text1", "\'sample text2\'", "sample text3"]

If we check the data type of "sample_str_to_list" above, it will be list.

I have tried to do it using string slicing, but it did not help. Can somebody please help here. Thank in advance.

Crusader
  • 313
  • 2
  • 7
  • 2
    Check [`ast.literal_eval`](https://docs.python.org/3/library/ast.html#ast.literal_eval) for a safer version than `eval`. – Guimoute Jan 19 '22 at 14:57
  • 4
    Are you sure you don't have a JSON value to decode? I would be surprised if you actually want to keep the backslashes, which aren't part of the original string, only part of the string-literal syntax to allow a single quote to be included in the literal. – chepner Jan 19 '22 at 14:58
  • You could do `sample_str[1:-1].split(',')` and take the extra quotes off, but getting what you need directly might be better - where does the original string come from? – doctorlove Jan 19 '22 at 15:00
  • Alternatively, you can take the long route by `.replace()` all the unwanted characters then converting it to a list i guess – SunAwtCanvas Jan 19 '22 at 15:01
  • `json.loads('["sample text1", "\'sample text2\'", "sample text3"]')` and/or `ast.literal_eval('["sample text1", "\'sample text2\'", "sample text3"]')` both should work. – JonSG Jan 19 '22 at 15:02

3 Answers3

4

What you have looks an awful lot like JSON:

>>> sample_str = '["sample text1", "\'sample text2\'", "sample text3"]'
>>> import json
>>> json.loads(sample_str)
['sample text1', "'sample text2'", 'sample text3']

If it's actually the representation of a Python str value, there's ast.literal_eval:

>>> import ast
>>> ast.literal_eval(sample_str)
['sample text1', "'sample text2'", 'sample text3']

If it's neither of the above, you're going to have to identify the encoding scheme and find a parser for it, or write your own.

chepner
  • 497,756
  • 71
  • 530
  • 681
0

you could use ast.list_eval, for a safer eval

from ast import literal_eval
sample_str = '["sample text1", "\'sample text2\'", "sample text3"]'
sample_str = literal_eval(sample_str)

Output:

['sample text1', "'sample text2'", 'sample text3']
XxJames07-
  • 1,833
  • 1
  • 4
  • 17
-2

You can try using exec

exec("""
sample_str_to_list = ["sample text1", "'sample text2'", "sample text3"]
""")
tax evader
  • 2,082
  • 1
  • 7
  • 9
  • oh, if not `eval` then `exec`, why? – buran Jan 19 '22 at 14:59
  • 1
    Do not use neither [`eval()`](https://docs.python.org/3/library/functions.html#eval) nor [`exec()`](https://docs.python.org/3/library/functions.html#exec), the only appropriate way to evaluate code is [`ast.literal_eval()`](https://docs.python.org/3/library/ast.html#ast.literal_eval). **BUT** better to avoid this at all and solve task using string parsing. – Olvin Roght Jan 19 '22 at 15:01