Okay, the problem I have here is that I’ve got this .txt file Python imports into a dictionary.
What is does is it takes the values from the text files, that are in this Specific format:
a 0.01
b 0.11
c 1.11
d 0.02
^ (In code format because It wouldn’t wouldn’t stack it like in the .txt, not actually code)
and then puts them into a dictionary like this:
d = { ‘a’:’0.01′, ‘b’:’0.11, ect….}
Well, I’m using this so that it will change whatever value the user inputs (Later in the script) into whatever is defined inside the dictionary.
The problem is if I try and make it incorporate a space, it just doesn’t work.
Like, I finished the letters, and their corresponding values in the .txt and began going onto symbols:
For example:
& &
* *
(so that when entered into the dictionary, the corresponding values are printed when I have it print the translated message) (I could change them up, but I decided to leave them as they are)
The problem arises when I try and have it make a space in the user input correspond to a space or another value in the translated message.
I tried leaving a row blank in my .txt, so that (space) is to (space)
But later, when it tried to load the .txt, it gave me an error, saying that: “need more than one value to unpack”
Can someone help me out?
EDIT: Adding code as requested.
TRvalues = {}
with open(r"C:\Users\Owatch\Documents\Python\Unisung Net Send\nsed.txt") as f:
for line in f:
(key, val) = line.split()
TRvalues[key] = val
if TRvalues == False:
print("\n\tError encountered while attempting to load the .txt file")
print("\n\t The file does not contain any values")
else:
print("Dictionary Loaded-")
Sample text file:
a 0.01
b 0.11
c 1.11
d 0.02
e 0.22
f 2.22
g 0.03
h 0.33
i 3.33
j 0.04
k 0.44
l 4.44
m 0.05
n 0.55
o 5.55
p 0.06
q 0.66
r 6.66
s 0.07
t 0.77
u 7.77
v 0.08
w 0.88
x 8.88
y 0.09
z 0.99
I get this error when I attempt to run the script:
Traceback (most recent call last):
File "C:/Users/Owatch/Documents/Python/Unisung Net Send/Input Encryption 0.02.py", line 17, in <module>
(key, val) = line.split()
ValueError: need more than 0 values to unpack
EDIT: Thanks for downvoting everybody! I’m now no longer able to ask questions.
Believe it or not I DO know the rules for this website, and DID research this before asking on Stack Overflow. It IS helpful for other people as well. What a nice community. Hopefully the people who answered did not downvote it. I appreciate what they did.
If I understand correctly, you’re trying to use a space both as an unquoted value and as a delimiter, which won’t work. I’d use the
csvmodule and its quoting rules. For example (assuming you’re using Python 3 from yourprintfunctions):with an input file of
gives