#๐ How to read certain part of line within txt file
37 messages ยท Page 1 of 1 (latest)
@dapper badger
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.
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?
You need to parse the line into the parts you need. The easiest way to do that here is to just use the string's split method to split the line on the | character.
so
Or use the CSV module since if your format is CSV with pipes.
for account in accounts.readline().split(" |")
You'd just iterate accounts like you were doing (but without the () at the end of accounts). Then split account.
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?
Did you try it?
if account.split(" |") == username: think about what this is checking.
hmm
split returns a list.
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.
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
You're welcome. And see here: https://docs.python.org/3/library/functions.html#open
!pastebin
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.
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.