0

Files to dictionaries (answered)

file = open ("pmoney.py","w") file.write("TLTF /n $500 /n Sens /n $300 /n Rawk /n $900") #example file created try: file = open("pmoney.py","r") temp = 0 mykeys = [] myvals = [] money = {} x = 0 #opened file and set a few variables for use later for line in file: if temp == 0: mykeys.append(line) temp =+ 1 elif temp == 1: myvals.append(line) temp =- 1 #takes each line of the file and appends it to #either a list for keys or a list for values, this goes #back and forth between them for word in mykeys: money[word] = myvals[x] x =+ 1 #takes each item in the list of keys then pairs it #with the item in the values list. We use x to make #sure key 0 is with value 0, then we increase x to #move to the next value when keys automatically #moves forward. finally: file.close() print(money) I receive an index error when this is run, and I'm honestly not sure why, since mykeys and myvals should have...

27th Sep 2019, 5:16 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
12 Respostas
+ 3
You have a few basic errors in there. Your test data, if you want it to have the values in multiple lines, you have to use \n (backslash) as endline, instead of /n. The increment and decrement operator is += and -= it won't work if you change the order.
27th Sep 2019, 9:06 PM
Tibor Santa
Tibor Santa - avatar
+ 2
The reason why you got the index error is because you only had a single line, nothing was ever appended to myvals so you were trying to index an empty array
27th Sep 2019, 9:08 PM
Tibor Santa
Tibor Santa - avatar
+ 2
With slight tweaking, that worked. Thanks
27th Sep 2019, 9:20 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
+ 1
For long codes, please try to write them in the code playground so's easier to read and test.
27th Sep 2019, 5:57 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
I was missing the parentheses on the end of my file.read, my apologies for everyone I pinged
28th Sep 2019, 9:59 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
0
...the same amount of items in them, and it shouldn't increase x until after it's used it to declare the index
27th Sep 2019, 5:18 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
0
Also, the comments are post production, so they didn't transfer right. Here's the code without post comments: file = open ("pmoney.py","w") file.write("TLTF /n $500 /n Sens /n $300 /n Rawk /n $900") try: file = open("pmoney.py","r") temp = 0 mykeys = [] myvals = [] money = {} x = 0 for line in file: if temp == 0: mykeys.append(line) temp =+ 1 elif temp == 1: myvals.append(line) temp =- 1 for word in mykeys: money[word] = myvals[x] x =+ 1 finally: file.close() print(money)
27th Sep 2019, 5:21 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
0
Normally I try not to post code if it's just me testing things, especially when you can push the three dots then copy text, then paste that in the playground with a line or two removed. Anyways, here https://code.sololearn.com/c6VxMwtQ8U1V/?ref=app Edit: I used to think the code had to be public to be shared prior to just doing that, so I guess disregard all that above, I'll use that from now on.
27th Sep 2019, 6:24 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
0
There's also this one where I try a while loop instead of a for loop https://code.sololearn.com/c9GvGnRp8H6b/?ref=app
27th Sep 2019, 7:26 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
0
Ok, so with a few print commands I found out the error is the lines being appended to the lists, now I'm even more confused
27th Sep 2019, 8:07 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
0
https://code.sololearn.com/c9GvGnRp8H6b/?ref=app Addition question for anyone still watching, I tried to get it to go back into the file afterwards and now at the very end it won't read the pmoney.py file. What did I mess up
28th Sep 2019, 9:50 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
0
It's at line 83, commenting it out makes the "error" go away. The "error" even though it doesn't look like a normal error, says, "<built-in method read of _io.TextIOWrapper object at 0x00A7AA30>"
28th Sep 2019, 9:55 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar