def view():
with open("passwords.txt", 'r') as f:
for line in f.readlines():
data = line.rstrip()
user, passw = data.split("|")
print("User:", user, "| Password:", passw)
Error message:
`File "c:\Users\Josh Lee\Python\Hello World\password_manager.py", line 27, in <module>
view()
File "c:\Users\Josh Lee\Python\Hello World\password_manager.py", line 8, in view
user, passw = data.split("|")
ValueError: too many values to unpack (expected 2)
This is my code. I have tried to change line 8, but it’ll say too little expected (expected: 2 got:1) and then it’ll show this. I’ve been stuck on this for a while and would like some pointers on where I can improve the code
Ah, but it is “too many values to unpack” not “too few”. So some lines have more than one "|".
@TheJoshDude : these things are easily diagnosed if you print out what you’re getting, as @MRAB suggests. If necessary, do the troublesome part in smaller steps. If it’s a long file, you might need like to save yourself some reading e.g.:
Or, kill two birds with one stone by incorporating some basic error handling in your code—this will print errors that occur and the lines on which they do (so you can check them in your file). Once you decide how you want to handle lines with more or fewer separators than you expect, you can add the appropriate code to the except block.
for idx, line in enumerate(f.readlines()):
data = line.rstrip()
try:
user, pass = data.split("|")
except ValueError as e:
print("Could not parse line {idx + 1}: {e}"
continue
print("User:", user, "| Password:", pass)
Also, there’s no benefit to using readlines() over just iterating over the file object directly; it is way less memory-efficient while requiring extra code. Additionally, unless you know the file will be in a dynamic OS locale encoding rather than a fixed one (presumably UTF-8), you’ll want to add the encoding arg to open() so you can handle passwords with special characters on systems that don’t happen to have the OS locale default to UTF-8 (or whatever you’re expecting). Thus:
def view():
with open("passwords.txt", 'r', encoding="UTF-8") as f:
for idx, line in enumerate(f):
data = line.rstrip()
try:
user, pass = data.split("|")
except ValueError as e:
print("Could not print line {idx + 1}: {e}"
else
print("User:", user, "| Password:", pass)