Trying to add a tuple to a list and the character ' gets added breaking the code

Viewed 44

I am trying to make a simple password manager (in python) and I have already made some code that stores the service name and the randomly generated password in a tuple. It is just a normal txt file. The issue I am having is that when I read the file line by line, and get the tuple (it looks like this - ("YouTube", "vwL4F7MC94VBERtS7xGdL2IugT2b3n!H"), and append it to my list, it shows up as this:

['("YouTube", "vwL4F7MC94VBERtS7xGdL2IugT2b3n!H")', '("Google", "tcOtnrc6ES9ZH%PdPb$m%8fvOT4uZiJt")']

I need it to look like this:

[("YouTube", "vwL4F7MC94VBERtS7xGdL2IugT2b3n!H"), ("Google", "tcOtnrc6ES9ZH%PdPb$m%8fvOT4uZiJt")]

As you can see the quotes appear before and after both tuples which means that when I go to list the service names, It doesn't list them properly as it thinks that the '(' is the first item in the tuple when I just want it to list for example: Google, Youtube. I have everything else working in my code and even tried manually changing list and it works fine. I tried to just remove the character ' but that doesn't work as those quote marks act like the quote marks around a string. Here is my code that reads the file and appends it to the list as a tuple:

#opening data
data = open("D:\Documents\Password Manager\data.txt", "r")

for line in data.readlines():
    list_services_passwords.append(line)
    print(line)

print(list_services_passwords)

data.close()

EDIT:

Here is the data.txt file content:

("YouTube", "vwL4F7MC94VBERtS7xGdL2IugT2b3n!H")

("Google", "tcOtnrc6ES9ZH%PdPb$m%8fvOT4uZiJt")

1 Answers
import ast

with open("D:\Documents\Password Manager\data.txt", "r") as fp:
    list_services_passwords = fp.readlines()

list_services_passwords = [ast.literal_eval(p) for p in list_services_passwords]

Output:

[('YouTube', 'vwL4F7MC94VBERtS7xGdL2IugT2b3n!H'), ('Google', 'tcOtnrc6ES9ZH%PdPb$m%8fvOT4uZiJt')]
Related