Reputation: 4420
So what I am trying to do is load up all my usernames and passwords from a textfile into a dictionary to call upon them...
def createacc():
x = 0
while x != 200:
print("\n")
x+=1
answer = {}
error = None
with open('usernames.dat', 'r') as document:
for line in document:
n = line.replace("\n'","")
arts = n.replace("[","")
parts = arts.replace("['","")
nparts = parts.split("=")
line = parts
if not line: # empty line?
continue
answer[line[0]] = line[1:]
print("\n\n\n\n\n")
print(answer)
print("\n\n\n\n\n")
if request.method == "POST":
if request.form['username'] == '' or request.form['password'] == "" or request.form['code'] != "30286":
error="Faliure to create account!! please try again"
else:
acc = open("usernames.dat","a+")
a = acc.readlines()
aaa = request.form['username'] +"="+ request.form['password'] + "\n"
acc.writelines(aaa)
acc.close()
return redirect(url_for('sms'))
return render_template('newacc.html',error=error)
And What I was hoping my dictionary out would look like would be like so
{'admin':'admin', 'PPGC': 'Default', 'DELTA': 'Readme'}
and the result I am getting is
{'a': 'dmin=admin\n', 'P': 'PGC\n', 'D': 'ELTA\n', 'N': 'AVI\n'}
What do I need to do to get my desired dictionary?
Upvotes: 1
Views: 137
Reputation: 401
Is your program writing the username, passwords in a textfile? If yes, I would suggest to use JSON or any other data interchange format. For example, with json you can do this
import json
user_dict = {'some user': 'some password'}
with open('my_database.json', 'w') as db:
json.dump(user_dict, db)
# Now the data can be can easily loaded
user_data = json.load(open('my_database.json'))
# This will get you the dictionary back.
Upvotes: 2