Connecting Tech Pros Worldwide Help | Site Map

importing a list of numbers from excel

  #1  
Old July 4th, 2009, 09:07 PM
Newbie
 
Join Date: Jul 2009
Posts: 22
I am new with python and programming in general so I am sorry if what I am about to say doesn't make any sense. I have several different lists of numbers in excel and I would like to save each individual list as something, eg. MyHighs. As of right now I have just put in the numbers my self as shown below...

My_High = (95, 96, 97, 95, 91, 93, 98, 97, 99, 101, 100, 100)
Actual_High = (100, 99, 95, 97, 91, 91, 102, 99, 100, 99, 99, 99)

I have those numbers listed in excel and I would like to save that list of numbers as My_High and Actual_High. However, I am not sure how you would import the excel file and then save the list of numbers of something. I appreciate any helpful hints!!
  #2  
Old July 5th, 2009, 02:21 AM
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,545

re: importing a list of numbers from excel


Let's say you have two columns in your worksheet. The first column is labeled My_High (cell A-1) and the second Actual_High (cell B-1). The numbers follow in the rows below. The easiest thing to do is save the worksheet as a csv (comma separated value) file and read the file with a python script. Save the data as a dictionary. Example code:
Expand|Select|Wrap|Line Numbers
  1. f = open('high.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()
  12.  
  13. for key in keys:
  14.     print "%s: %s" % (key, dd[key])
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> My_High: [95, 96, 97, 95, 91, 93, 98, 97, 99, 101, 100, 100]
  2. Actual_High: [100, 99, 95, 97, 91, 91, 102, 99, 100, 99, 99, 99]
  3. >>> 
  #3  
Old July 5th, 2009, 08:30 AM
Newbie
 
Join Date: Jul 2009
Posts: 22

re: importing a list of numbers from excel


I put in the code you gave me and as an output I got this...

99: [ ]
98: [ ]
755555543cleaclear: [ ]

Not sure what any of that means or why I wasn't getting my list of numbers to show up. Thanks for all your help, I really appreciate it.
  #4  
Old July 5th, 2009, 03:05 PM
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,545

re: importing a list of numbers from excel


You have other data in your worksheet and it is not formatted the way I described. The CSV file should look like this for my code to work:

My_High,Actual_High
95,100
96,99
97,95
95,97
91,91
93,91
98,102
97,99
99,100
101,99
100,99
100,99

If you could show me how your data looks, I might be able to suggest something.
  #5  
Old July 5th, 2009, 10:00 PM
Newbie
 
Join Date: Jul 2009
Posts: 22

re: importing a list of numbers from excel


This is how my excel sheet looks. My Highs is in column A and Actual Highs is in column B. Hope this makes sense. Thanks.

My Highs Actual Highs
98 99
99 99
100 99
100 100
99 99
101 102
91 91
92 91
97 97
96 95
98 99
100 100
97 95
86
  #6  
Old July 5th, 2009, 10:51 PM
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,545

re: importing a list of numbers from excel


Your CSV file should look like this:

My Highs,Actual Highs
98,99
99,99
100,99
100,100
99,99
101,102
91,91
92,91
97,97
96,95
98,99
100,100
97,95
86

My code produces the following output:
Expand|Select|Wrap|Line Numbers
  1. >>> My Highs: [98, 99, 100, 100, 99, 101, 91, 92, 97, 96, 98, 100, 97, 86]
  2. Actual Highs: [99, 99, 99, 100, 99, 102, 91, 91, 97, 95, 99, 100, 95]
  3. >>> 
Open your CSV file and look at the contents.
  #7  
Old July 6th, 2009, 12:31 AM
Newbie
 
Join Date: Jul 2009
Posts: 22

re: importing a list of numbers from excel


When I open my file, verification.csv in excel it looks like what i posted above. When I open that same file in notepad it looks exactly like yours. In my code I have...f = open("verification.csv")...I then tried saving it as a .txt file in notepad but I get the same output.
  #8  
Old July 6th, 2009, 02:57 AM
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,545

re: importing a list of numbers from excel


Are you saving the excel file (xls extension) from the File menu with Save As and selecting CSV as the "Save as type"?
  #9  
Old July 6th, 2009, 04:45 AM
Newbie
 
Join Date: Jul 2009
Posts: 22

re: importing a list of numbers from excel


I am saving it as CSV (Comma delimited). Other CSV choices are...CSV (MS-DOS) and CSV (Macintosh). However, I do not have a mac and I also tried saving it as CSV (MS-DOS) but that did not work either.
  #10  
Old July 6th, 2009, 02:55 PM
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,545

re: importing a list of numbers from excel


I cannot explain the problem you are having. I have an application that uses a similar method of reading spreadsheet data to input structural steel building columns into steel detailing software. The CSV file looks like this:

Grid,ColSize,Grade,BottElev,TopElev,Rotation,BaseP late
E-2,W10x33,A992,-3'-4,26'-2 1/2,0,BP6
E-3,W10x33,A992,-3'-4,26'-7 7/8,90,BP6
E-3.8,W10x33,A992,-3'-4,33'-3 3/4,90,BP6
E-4.6,W10x33,A992,-3'-4,33'-8 1/8,90,BP6
E-5.7,W10x33,A992,-3'-4,34'-2 1/16,90,BP6
.....................

I can assure you, it works. Try entering the complete path:

f = open("C:\\dir1\\dir2\\verification.csv")
  #11  
Old July 6th, 2009, 06:54 PM
Newbie
 
Join Date: Jul 2009
Posts: 22

re: importing a list of numbers from excel


I typed in the complete path and it worked!! Thanks!
Reply

Tags
excel, i/o, import, list of numbers


Similar Threads
Thread Thread Starter Forum Replies Last Post
importing contacts I can only get first 3 phone numbers? =?Utf-8?B?bW9uaWNhX2ZyYXppZXI=?= answers 1 January 31st, 2008 05:05 AM
Strange Excel importing problem Rob T answers 5 November 21st, 2005 07:39 PM
Problem importing and querying after five integers (MS Access 2002) Emilio answers 1 November 12th, 2005 02:09 PM
author index for Python Cookbook 2? Andrew Dalke answers 10 July 19th, 2005 12:26 AM