0

First of all: apologies if this question has been already asked. I have tried finding it on the site for quite some time now.

Essentially the problem is very simple: I have a text file that is already formatted like a python list: ["test1", "test2", "test3", "test4"]

I'm simply looking for the most simple way to parse this into a variable, ie.

print(testVariableOne)
>["test1", "test2", "test3", "test4"]

print(testVariableOne[0])
>["test1"]

I was thinking maybe of using regex to split each element, but this seems a bit convoluted.

Apologies for the (almost definitely) repeated question.

Nova
  • 3
  • 1

1 Answers1

0

With ast.literal_eval you can safely evaluate an expression node or a string containing a Python literal or container display.

something like this :

import ast
file_content='["test1", "test2", "test3", "test4"]'
file_content=ast.literal_eval(file_content)

and your output will be a list .

Ata Reenes
  • 81
  • 8