By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,050 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,050 IT Pros & Developers. It's quick & easy.

Help with calcuations using imported numbers.

P: 22
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...

Expand|Select|Wrap|Line Numbers
  1. f = open("C:\users\cory\desktop\Verification.csv")
  2. dd = {}
  3. keys = f.readline().strip().split(',')
  4. for key in keys:
  5.     dd.setdefault(key, [])
  6.  
  7. for line in f:
  8.     for i, item in enumerate(line.strip().split(',')):
  9.         dd[keys[i]].append(int(item))
  10.  
  11. f.close()
I don't know where to begin.

Thanks!
Jul 6 '09 #1
Share this Question
Share on Google+
6 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Please use code tags when posting code. See this link.

What is delta.days?

Access the list My_High like this:
Expand|Select|Wrap|Line Numbers
  1. dd['My_High']
Jul 6 '09 #2

P: 22
delta.days is just the number of days in between two dates...

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. today = datetime.date.today()
  4. date_format = "%m/%d/%Y"
  5. d0 = date(2009, 6, 21)
  6. d1 = today
  7. delta = d1-d0
  8. print delta.days
  9.  
I have this as my code now...

Expand|Select|Wrap|Line Numbers
  1.  
  2. f = open("C:\users\cory\desktop\Verification.csv")
  3. dd = ['My_Highs']
  4. keys = f.readline().strip().split(',')
  5. for key in keys:
  6.     dd.setdefault(key, ['My_Highs'])
  7.  
  8. for line in f:
  9.     for i, item in enumerate(line.strip().split(',')):
  10.         dd[keys[i]].append(int(item))
  11.  
  12. f.close()
  13.  
  14.  
...and I am getting this error message...Attribute Error: 'list' object has no attribute 'setdefault'
Jul 6 '09 #3

bvdet
Expert Mod 2.5K+
P: 2,851
Evidently you did not look at the code to understand what is happening. I will attempt to explain with embedded comments:
Expand|Select|Wrap|Line Numbers
  1. # Create an open file object for reading.
  2. f = open('your_file.csv')
  3. # Initialize an empty dictionary.
  4. dd = {}
  5. # Read the first line in the file.
  6. # These are the column labels.
  7. # We will use these labels as the dictionary keys.
  8. # Create a list by splitting the string on the comma.
  9. keys = f.readline().strip().split(',')
  10. # Iterate on the list of labels.
  11. for key in keys:
  12.     # Using dictionary method setdefault, assign each dictionary
  13.     # key to an empty list.
  14.     dd.setdefault(key, [])
  15.  
  16. # Iterate on the file object.
  17. # This iteration begins with line 2 because we already read the first line.
  18. for line in f:
  19.     # Create a list of scores out of each line by splitting the string
  20.     # on the comma character. Iterate on the list of scores.
  21.     for i, item in enumerate(line.strip().split(',')):
  22.         # Append each score to the value of the corresponding 
  23.         # dictionary key. Type cast the score to int.
  24.         dd[keys[i]].append(int(item))
  25.  
  26. # Close the file object.
  27. f.close()
  28.  
  29. # Iterate on the list of keys (labels).
  30. for key in keys:
  31.     # Print the key and corresponding dictionary value.
  32.     print "%s: %s" % (key, dd[key])
Study the comments above and post your question again.
Jul 6 '09 #4

P: 22
The comments helped a lot, I figured it out! Thanks for all your help.
Jul 6 '09 #5

bvdet
Expert Mod 2.5K+
P: 2,851
You are welcome. I think you learned something.

-BV
Jul 6 '09 #6

P: 22
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.
Jul 6 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.