Feb-02-2017, 09:46 PM
Thank you for your feedback. You are providing some good guidance that is forcing me to think about how the code behaves. I have redone the code yet again and I feel like I a farther from the solution because I can't even create a dictionary with the information that is stored in the now csv file. Any feedback below would be helpful. It may look like homework, but I am trying to dumb down the example so it is easier for folks to help me out. My loop is clearly not appending to the dictionary.
Filename: pricelist.csv
Part Number Amount
A 10
A 12
A 19
A 12
B 19
B 13
B 10
#Code:
#Create a dictionary.
SSPList = {}
#open a file that contains only the Part No and the price.
with open("pricelist.csv", "r") as f:
#Read each line in the file and add the items to the dictionary above.
for line in f:
PartNos, Price = line.strip().split(",")
Entry = {PartNos: str(Price)}
SSPList.update(Entry)
print (SSPList) #Epic fail. Only prints the last item for each unique PartNo. What am I doing wrong?
#Calculate the average for each PartNo. Can't do this until I fix the dictionary.
f.close()
#Output:
>>>
{'A': '12', 'B': '10'}
>>>
Filename: pricelist.csv
Part Number Amount
A 10
A 12
A 19
A 12
B 19
B 13
B 10
#Code:
#Create a dictionary.
SSPList = {}
#open a file that contains only the Part No and the price.
with open("pricelist.csv", "r") as f:
#Read each line in the file and add the items to the dictionary above.
for line in f:
PartNos, Price = line.strip().split(",")
Entry = {PartNos: str(Price)}
SSPList.update(Entry)
print (SSPList) #Epic fail. Only prints the last item for each unique PartNo. What am I doing wrong?
#Calculate the average for each PartNo. Can't do this until I fix the dictionary.
f.close()
#Output:
>>>
{'A': '12', 'B': '10'}
>>>