I seek to make a simple script that does the following:
Open a file called Accounts2.txt that contains a list of lists in string form. Each entry in the bigger list is a smaller list in the format of [username,password].
Turn the Accounts2 string into a list of lists that python can manipulate.
Check each entry in the big list to see if it matches [CheckName,Checkpass]. If it does, then login is successful. If it doesnt, then it goes to the next entry in the list. If it reaches the end of the list, then it says login unsuccessful.
CheckName = "user3"
CheckPass = "pass3"
path = "C:\\Users\\drago\\Desktop\\PythonPractice\\Accounts2.txt"
InFile = open(path, "r")
CredentialsString = str(InFile.read())
print(CredentialsString)
InFile.close()
CredentialsList = CredentialsString.split("],[")
i=0
for i in range(len(CredentialsList)):
print(CredentialsList[i])
if [CheckName,CheckPass] == CredentialsList[i]:
print("Login successful")
else: print("Login unsuccessful")
I'm stuck on part 2, as i'm not sure how to turn it into a list of lists using the split function without it removing parts of the formatting.
I am also stuck on part 3, as it prints 'login unsuccesful' at each check rather than doing it at the end if it fails every check.
Preferred if any solutions can be reasonably understood at my skill level. The difference between my question and past questions is that I don't understand the use of ast.literaleval or any similar imported library functions. If that can be explained that would be good.
Edit: Tried doing this:
import ast
CredentialsList = ast.literal_eval(CredentialsString)
print(CredentialsList)
which returns this error message:
File "C:\Users\drago\OneDrive\Desktop\Python374\Softwares\Lib\ast.py", line 71, in _raise_malformed_node
raise ValueError(msg + f': {node!r}')
ValueError: malformed node or string on line 1: <ast.Name object at 0x000002E4CADBD5D0>