Help with calcuations using imported numbers.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mott3510
    New Member
    • Jul 2009
    • 22

    Help with calcuations using imported numbers.

    I have imported two columns of numbers from excel and now i need to use those two columns of numbers to calculate the following...

    Average_Error = sum(map(lambda a, b: abs(a - b), My_High, Actual_High))/delta.days

    I need the first column to be inserted where I have My_High in the code above, and I need the second column to be inserted where I have Actual_High in the code above. I used the following code to import the numbers...

    Code:
    f = open("C:\users\cory\desktop\Verification.csv")
    dd = {}
    keys = f.readline().strip().split(',')
    for key in keys:
        dd.setdefault(key, [])
     
    for line in f:
        for i, item in enumerate(line.strip().split(',')):
            dd[keys[i]].append(int(item))
     
    f.close()
    I don't know where to begin.

    Thanks!
    Last edited by bvdet; Jul 6 '09, 07:47 PM. Reason: Added code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Please use code tags when posting code. See this link.

    What is delta.days?

    Access the list My_High like this:
    Code:
    dd['My_High']

    Comment

    • mott3510
      New Member
      • Jul 2009
      • 22

      #3
      delta.days is just the number of days in between two dates...

      Code:
       
      
      today = datetime.date.today()
      date_format = "%m/%d/%Y"
      d0 = date(2009, 6, 21)
      d1 = today
      delta = d1-d0
      print delta.days
      I have this as my code now...

      Code:
      f = open("C:\users\cory\desktop\Verification.csv")
      dd = ['My_Highs']
      keys = f.readline().strip().split(',')
      for key in keys:
          dd.setdefault(key, ['My_Highs'])
       
      for line in f:
          for i, item in enumerate(line.strip().split(',')):
              dd[keys[i]].append(int(item))
       
      f.close()
      ...and I am getting this error message...Attri bute Error: 'list' object has no attribute 'setdefault'

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Evidently you did not look at the code to understand what is happening. I will attempt to explain with embedded comments:
        Code:
        # Create an open file object for reading.
        f = open('your_file.csv')
        # Initialize an empty dictionary.
        dd = {}
        # Read the first line in the file.
        # These are the column labels.
        # We will use these labels as the dictionary keys.
        # Create a list by splitting the string on the comma.
        keys = f.readline().strip().split(',')
        # Iterate on the list of labels.
        for key in keys:
            # Using dictionary method setdefault, assign each dictionary
            # key to an empty list.
            dd.setdefault(key, [])
        
        # Iterate on the file object.
        # This iteration begins with line 2 because we already read the first line.
        for line in f:
            # Create a list of scores out of each line by splitting the string
            # on the comma character. Iterate on the list of scores.
            for i, item in enumerate(line.strip().split(',')):
                # Append each score to the value of the corresponding 
                # dictionary key. Type cast the score to int.
                dd[keys[i]].append(int(item))
        
        # Close the file object.
        f.close()
        
        # Iterate on the list of keys (labels).
        for key in keys:
            # Print the key and corresponding dictionary value.
            print "%s: %s" % (key, dd[key])
        Study the comments above and post your question again.

        Comment

        • mott3510
          New Member
          • Jul 2009
          • 22

          #5
          The comments helped a lot, I figured it out! Thanks for all your help.

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            You are welcome. I think you learned something.

            -BV

            Comment

            • mott3510
              New Member
              • Jul 2009
              • 22

              #7
              I did! This is only my third day working with python, and I have never written code before so I will more than likely have some more questions with due time.

              Comment

              Working...