2

I have a python file that requires a username and password for authentication. These username and passsword details are saved to the file in the format

    userlist = [[username, password], [username1, password1]]

and so on. This works but when I write my list to a file, I will write it as a string like this:

    name_write_file = open("names.txt", "w")
    name_write_file.writelines(str(name_list))
    print("user data successfully saved to file")
    name_write_file.close()

Later on in the code, I need to recognise a username in order to initiate the login sequence and I do this using

    userlist[0] 

Normally this should return "username" but instead it now returns "[" (the first parenthesis from the string saved to file)

Any way to fix this?

Thanks in advance

Max Davies
  • 148
  • 10

4 Answers4

2

A quick way to save and retrieve a list is to use eval:

name_list = [['username', 'password'], ['username1', 'password1']]

name_write_file = open("names.txt", "w")
name_write_file.writelines(str(name_list))
print("user data successfully saved to file")
name_write_file.close()

with open("names.txt") as f:
   name_list2 = eval(f.read())

print(name_list)
print(name_list2)
print(name_list2[0])

Output

[['username', 'password'], ['username1', 'password1']]
[['username', 'password'], ['username1', 'password1']]
['username', 'password']
Mike67
  • 11,175
  • 2
  • 7
  • 15
1

file.writelines(sequence) As you know, it saves the item in sequence as a string in the file. So to solve your problem, you need to write some tricky code like the following. `

userlist = ["user1-password1\n", "user2-password2"]
# save user data
f = open("test.txt", "w+")
f.writelines(userlist)
f.close()
# get user data
f = open("test.txt", "r+")
read_list = f.readlines()
user_data = [[item.split('-')[0], item.split('-')[1].rstrip()]for item in read_list]
print(user_data)
f.close()

` You can have some ideas from this code. Of course, you can improve it.

Senior0023
  • 142
  • 1
  • 9
1

file.writelines(sequence) As you know, it saves the item in sequence as a string in the file. So to solve your problem, you need to write some tricky code like the following. `

userlist = ["user1-password1\n", "user2-password2"]
# save user data
f = open("test.txt", "w+")
f.writelines(userlist)
f.close()
# get user data
f = open("test.txt", "r+")
read_list = f.readlines()
user_data = [[item.split('-')[0], item.split('-')[1].rstrip()]for item in read_list]
print(user_data)
f.close()

` You can have some ideas from this code. Of course, you can improve it.

The built-in eval() function is also a good approach for your solution.

Senior0023
  • 142
  • 1
  • 9
0

A CSV file is a better format to save your data. You can write your data like this:

with open("my_user_pwd.csv", 'w', newline='') as csvfile:
    fieldnames = ['username', 'password']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writerow({'Username1': 'Password1'})
    writer.writerow({'Username2': 'Password2'})
Karl
  • 481
  • 1
  • 6
  • 13