#๐Ÿ”’ How to read certain part of line within txt file

37 messages ยท Page 1 of 1 (latest)

finite fogBOT
#

@dapper badger

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

dapper badger
#
def passwordValidation(password):     
    upperCaseCheck = False
    symbolCheck = False
    numberCheck = False 
    for letter in password:
        if letter.isupper():
            upperCaseCheck = True
        elif letter.isalnum():
            symbolCheck = True
        elif letter.isdigit():
            numberCheck = True
    if len(password) < 8:
        return "\nPassword must be longer than 8 characters."
    elif not upperCaseCheck:
        return "\nPassword must contain a capital letter."
    elif not symbolCheck:
        return "\nPassword must contain a special character."
    elif not numberCheck:
        return "\nPassword must contain a number"
    else:
        return "\nAccount Successfuly created."

# Login Screen
def accountRegistration():
    global username
    with open("Accounts.json", "ar") as accounts:
        if username == "":
            username = takeInput(str, "\nPlease select a username. must be unique. > ")
            for account in accounts():
                if account == username:
                    username = ""
                    return "\nUsername in use."
        else:
            password = takeInput(str, "\nPlease input a password.")
            print(passwordValidation)

        accounts.append(f"{username}|{password}")
#

code

#

on line for account in accounts

#

i would like it to only check the part before the | on each line so that it doesnt also read the password. how could i go about doing this?

remote prawn
dapper badger
#

so

remote prawn
#

Or use the CSV module since if your format is CSV with pipes.

dapper badger
#

for account in accounts.readline().split(" |")

remote prawn
#

You'd just iterate accounts like you were doing (but without the () at the end of accounts). Then split account.

dapper badger
#
def accountRegistration():
    global username
    with open("Accounts.json", "ar") as accounts:
        if username == "":
            username = takeInput(str, "\nPlease select a username. must be unique. > ")
            for account in accounts:
                if account.split(" |") == username:
                    username = ""
                    return "\nUsername in use."
        else:
            password = takeInput(str, "\nPlease input a password.")
            if validPassword:
                accounts.append(f"{username}|{password}")
                return passwordValidation
            else:
                return passwordValidation
#

looks good?

remote prawn
#

Did you try it?

dapper badger
#

not yet aha

#

its not implemented into the program as of yet

remote prawn
#

if account.split(" |") == username: think about what this is checking.

dapper badger
#

hmm

remote prawn
#

split returns a list.

dapper badger
#

usernamepass

#

ohh

#

if account.split(" |")[0] == username:

#

?

remote prawn
#

That looks better, ya. I'd move the split out for clarity though:

read_username, _ = account.split(" |")
# or just
# read_username = account.split(" |")[0]
if read_username == username:

And I think you want to remove that space in the string since it doesn't look like your files contain a space there.

dapper badger
#

done and done

#

thank you very much

dapper badger
#

Hi i am getting one more error when attempting to open /create the file "accounts.json" i get the error [must have exactly one of create/read/write/append mode

#

with open("Accounts.json", "ar") as accounts:

#

how do i open the file in append and read mode

#

so that i can scan the file whether the username is taken

#

as well as write to the file if i want to add to it

remote prawn
dapper badger
#

perf

#

ty

dapper badger
#

!pastebin

finite fogBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

finite fogBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.