0

I have some problems with using a list in python. Right now, I open a .txt file with data, and read it into my python file. However, when I put the input from the datafile into variable data and print this to check if it works or not, I see a lot of extra brackets which I don't want. Right now, it looks like:

["['sports','pizza','other']"]

and I want it to have it in a way like this:

['sports','pizza','other']

Can someone help me to get this work? Reason why I want it in a format like I mentioned above, is that I want to compare the list with another list, and that does not work in the format with the ]"]

I hope someone will help me.

Max
  • 53
  • 1
  • 7

1 Answers1

0

Simply use eval function from Python.

>>> a = ["['sports','pizza','other']"]
>>> eval(a[0])
['sports', 'pizza', 'other']
Firman
  • 928
  • 7
  • 15
  • 1
    You probably rather want [ast.literal_eval](https://docs.python.org/3/library/ast.html#ast.literal_eval) – syntonym Sep 11 '17 at 09:32
  • Hmm.. Still doesn't work. I will edit the post with my code, maybe you can see then whats wrong? – Max Sep 11 '17 at 09:45