0

Working on a project creating a python flask website that stores user logins into a text file. I have a text file where each line is one user and each user has 5 parameters stored on the line. All user parameters are separated by a ; character.

Parameters are:

username
password
first name
last name
background color
title
avatar

Sample of the text file:

joebob;pass1;joe;bob;yellow;My title!!;https://upload.wikimedia.org/wikipedia/commons/c/cd/Stick_Figure.jpg
richlong;pass2;rich;long;blue;My title2!!;https://www.iconspng.com/images/stick-figure-walking/stick-figure-walking.jpg

How do I go about storing the parameters into a python array, and how do I access them later when I need to reference log-ins.

Here is what I wrote so far:

accounts = { }
def readAccounts():
    file = open("assignment11-account-info.txt", "r")
    for accounts in file: #line
        tmp = accounts.split(';')
        for data in tmp: #data in line
            accounts[data[0]] = {
                'user': data[0],
                'pass': data[1],
                'first': data[2],
                'last': data[3],
                'color': data[4],
                'title': data[5],
                'avatar': data[6].rstrip()
            }
        file.close()
1
  • 1
    Your username is unique?? Commented May 1, 2020 at 4:37

2 Answers 2

1

You can use the python builtin csv to parse

import csv

with open("assignment11-account-info.txt", "r") as file:
    reader = csv.reader(file, delimiter=';')
    result = []
    for row in reader:
        fields = ('user', 'passwd', 'first', 'last', 'color','title','avatar')
        res = dict(zip(fields, row))
        result.append(res)

Or equivalent but harder to read for a beginner the pythonic list comprehension:

with open("assignment11-account-info.txt", "r") as file:
        reader = csv.reader(file, delimiter=';')
        fields = ('user', 'passwd', 'first', 'last', 'color','title','avatar')
        result = [ dict(zip(fields, row)) for row in reader ]
Sign up to request clarification or add additional context in comments.

Comments

1

Here's what I might do:

accounts = {}
with open("assignment11-account-info.txt", "r") as file:
  for line in file:
    fields = line.rstrip().split(";")
    user = fields[0]
    pass = fields[1]
    first = fields[2]
    last = fields[3]
    color = fields[4]
    title = fields[5] 
    avatar = fields[6]
    accounts[user] = {
      "user" : user,
      "pass" : pass,
      "first" : first,
      "last" : last,
      "color" : color,
      "title" : title,
      "avatar" : avatar
    }

By using with, the file handle file is closed for you automatically. This is the most "Python"-ic way of doing things.

So long as user is unique, you won't overwrite any entries you put in as you read through the file assignment11-account-info.txt.

If you need to deal with a case where user is repeated in the file assignment11-account-info.txt, then you need to use an array or list ([...]) as opposed to a dictionary ({...}). This is because reusing the value of user will overwrite any previous user entry you add to accounts. Overwriting existing entries is almost always a bad thing when using dictionaries!

If that is the case, I might do the following:

accounts = {}
with open("assignment11-account-info.txt", "r") as file:
  for line in file:
    fields = line.rstrip().split(";")
    user = fields[0]
    pass = fields[1]
    first = fields[2]
    last = fields[3]
    color = fields[4]
    title = fields[5] 
    avatar = fields[6]
    if user not in accounts:
      accounts[user] = []
    accounts[user].append({
      "user" : user,
      "pass" : pass,
      "first" : first,
      "last" : last,
      "color" : color,
      "title" : title,
      "avatar" : avatar
    })

In this way, you preserve any cases where user is duplicated.

1 Comment

The lookup is definitely better with this dictionary store.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.