Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Average
#6
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'}
>>>
Reply


Messages In This Thread
Simple Average - by ddoogles - Feb-02-2017, 05:40 PM
RE: Simple Average - by buran - Feb-02-2017, 05:45 PM
RE: Simple Average - by ddoogles - Feb-02-2017, 06:23 PM
RE: Simple Average - by buran - Feb-02-2017, 07:12 PM
RE: Simple Average - by wavic - Feb-02-2017, 07:49 PM
RE: Simple Average - by ddoogles - Feb-02-2017, 09:46 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020