473,327 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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...

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
6 1471
bvdet
2,851 Expert Mod 2GB
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
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
2,851 Expert Mod 2GB
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
The comments helped a lot, I figured it out! Thanks for all your help.
Jul 6 '09 #5
bvdet
2,851 Expert Mod 2GB
You are welcome. I think you learned something.

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

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

Similar topics

0
by: abcd | last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a ljetvedehg, hard slog," mariyu vede legai pressed...
4
by: Skully Matjas | last post by:
I am using the following code (created by the wizard) to allow to bring my form to a particular entery. But when I edit the entery (ex: put new information into a blank cell), it puts that record...
1
by: Skully Matjas | last post by:
Thank you for getting back to me. I am very new at this so i didnot understand what you said, here i will give as much cetails as possible: 1) The combo box i am using is combox39 2) I imported...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
5
by: Kosmos | last post by:
Hey :) hopefully someone can help me with this...I decided to take on the task of programming an access database for my legal co-op/internship...I'm studying law and music production on the...
1
by: tom.youdan | last post by:
Hi, I have an access dbs with 2 key tables. One is a State table that has State id and State name fields. This feeds a combo box in a form to provide a reference point for the State Id field in...
1
by: jmalone | last post by:
I have a python script that I need to freeze on AIX 5.1 (customer has AIX and does not want to install Python). The python script is pretty simple (the only things it imports are sys and socket)....
1
by: Constantine AI | last post by:
I am wanting to import CSV files into Access, which isn't a problem at the moment the code i have is as follows: Dim strSQL As String Dim CSVTable As String Dim FilePath As String ...
1
by: sanju4kk | last post by:
I am attempting to create a module that I can call to create a new field in my temp table that will be an autonumber, and then assigned that new field as the primary key. I have been search high and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.