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

importing a list of numbers from excel

P: 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!!
Jul 4 '09 #1
Share this Question
Share on Google+
10 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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. >>> 
Jul 5 '09 #2

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

bvdet
Expert Mod 2.5K+
P: 2,851
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.
Jul 5 '09 #4

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

bvdet
Expert Mod 2.5K+
P: 2,851
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.
Jul 5 '09 #6

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

bvdet
Expert Mod 2.5K+
P: 2,851
Are you saving the excel file (xls extension) from the File menu with Save As and selecting CSV as the "Save as type"?
Jul 6 '09 #8

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

bvdet
Expert Mod 2.5K+
P: 2,851
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")
Jul 6 '09 #10

P: 22
I typed in the complete path and it worked!! Thanks!
Jul 6 '09 #11

Post your reply

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