I'm currently trying to load a .txt file that has the following inside the file:
['Chest', ['bench', 'incline'], [2, 1], [10, 10], [10, 20, 10]], 'Chest', ['decline'], [1], [1], [10]
When I load the file, read the information on the file and store it on a variable named content
:
self.file_name = filedialog.askopenfilename()
if self.file_name is None:
return
with open(self.file_name) as f:
content = f.read().splitlines()
When I print out content:
print(content)
I get the following output:
["['Chest', ['bench', 'incline'], [2, 1], [10, 10], [10, 20, 10]], 'Chest', ['decline'], [1], [1], [10]"]
The problem is that there's quotation marks when it prints. Could there be anyway to get rid of the ""? The reason is because since it's a two dimensonal list and print([0][1])
I get the result of ' instead of chest
If your content contains syntax representing correct python literal code, you can parse it directly into python data:
content = ["['Chest', ['bench', 'incline'], [2, 1], [10, 10], [10, 20, 10]], 'Chest', ['decline'], [1], [1], [10]"]
import ast
a_tuple = ast.literal_eval(content[0])
print(a_tuple)
Results in a tupple containing the parased string:
(['Chest', ['bench', 'incline'], [2, 1], [10, 10], [10, 20, 10]], 'Chest', ['decline'], [1], [1], [10])