473,394 Members | 2,048 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,394 software developers and data experts.

importing a list of numbers from excel

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
10 2830
bvdet
2,851 Expert Mod 2GB
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
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
2,851 Expert Mod 2GB
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
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
2,851 Expert Mod 2GB
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
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
2,851 Expert Mod 2GB
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
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
2,851 Expert Mod 2GB
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
I typed in the complete path and it worked!! Thanks!
Jul 6 '09 #11

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

Similar topics

1
by: Emilio | last post by:
(MS Access 2002) Hello, I'm working with some big Census (PUMS) files, and I run into a peculiar problem once the data field exceeds five integers. I'll explain every step, since I am doing it in...
7
by: Darren | last post by:
I have been attempting to create a reservation planning form in excel that imports Data from an Access database and inserts that information automaticly into the correct spreed sheet and the...
5
by: Rob T | last post by:
I have a routine that imports a list of part numbers into a dataview: strExcel = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & boxFile.Text & ";Extended Properties=""Excel 8.0;HDR=NO"""...
2
by: jason.teen | last post by:
Hi, I am having trouble importing a spreadsheet from Excel into an Access Database. I have noticed that even specifying the Cell Type of the Excel Data Cell, When I import it, still get...
6
by: Michael Peters | last post by:
the Excel data that I would like to import into Access sometimes contain line breaks, like this: 623 749 in one cell. In Access, the imported data looks like this then: 623749
2
by: RRoma | last post by:
Does anybody know how I can import lists of numbers from Excel into Access without losing the preceeding Zeros??? The field must have a total of 10 digits.... Is this a function of how its saved in...
28
by: kkadakia | last post by:
I get a daily excel file for a entire month which I want to transfer into Access at the end of the month. So, there are around 20-25 excel files I get by the end of the month, and I would like to...
1
by: =?Utf-8?B?bW9uaWNhX2ZyYXppZXI=?= | last post by:
When I import contacts from excel using the import wizard, even after matching fields to field for some reason I can only get the first three to five from my list of 190 to import the phone numbers...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.