444,050 Members | 1,009 Online
+ 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 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! Jul 6 '09 #1
6 Replies

 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 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     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... Expand|Select|Wrap|Line Numbers   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...Attribute Error: 'list' object has no attribute 'setdefault' Jul 6 '09 #3

 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 # 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. Jul 6 '09 #4

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

 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