1

I am writing a python code for beam sizing. I have an Excel workbook from AISC that has all the data of the shapes and other various information on the cross-sections. I would like to be able to reference data in particular cells in this Excel workbook in my python code.

For example if the width of rectangle is 2in and stored in cell A1 and the height is 10in and stored in cell B1 I want to write a code in python that somehow pulls in cell A1 and B1 and multiply them.

I do not need to export back into excel I just want to make python do all the work and use excel purely as reference material.

Thank you in advance for all your advice and input!!

1
  • ok, making it a CSV is easy I am just not sure how I would reference a certain cell of that file. Commented Jan 5, 2016 at 2:13

4 Answers 4

3

Try pandas as well...might be easier to work with than lists in this case

DATA :

Width   Height
4           2
4           4
1           1
4           5

Code

import pandas as pd

#read the file
beam = pd.read_csv('cross_section.csv')         

beam['BeamSize'] = beam['Width']*beam['Height']  #do your calculations

Output:

>>> beam
   Width  Height  BeamSize
0      4       2         8
1      4       4        16
2      1       1         1
3      4       5        20
4      2       2         4

You can slice and dice the data as you wish. For eg, lets say you want the fifth beam :

>>> beam.ix[4]
Width       2
Height      2
BeamSize    4
Name: 4, dtype: int64

Check this for more info: http://pandas.pydata.org/pandas-docs/stable/

You can read directly from excel as well..

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for this idea. I will definitely look into this. My next challenge is learning to do a VLOOKUP of sorts in python using either Pandas or Numpy.
@JustinKordas I have to use Excel constantly to do analytics. I import everything exactly like astrosyam is referencing and it is 100x easier. Use Pandas if you have the opportunity
@JeremyLewallen I will be watching some videos on pandas today and learning how to use pandas. Thank you!!
0

Thank you for you inputs. I have found the solution I was looking for by using Numpy.

data = np.loadtxt('C:\Users[User_Name]\Desktop[fname].csv', delimiter=',')

using that it took the data and created an array with the data that I needed. Now I am able to use the data like any other matrix or array.

Comments

0

If you don't mind adding a (somewhat heavy) dependency to your project, pandas has a read_excel function that can read a sheet from an Excel workbook into a pandas DataFrame object, which acts sort of like a dictionary. Then reading a cell would just amount to something like:

data = pd.read_excel('/path/to/file.xls')
cell_a1 = data.loc[1, 'a']  # or however it organizes it on import

Comments

0

For future readers of this question, it should be mentioned that xlrd is the "most exact" solution to your requirements. It will allow you to read data directly from an Excel file (no need to convert to CSV first).

Many other packages that read Excel files (including pandas) use xlrd themselves to provide that capability. They are useful, but "heavier" than xlrd (larger, more dependencies, may require compilation of C code, etc.).

(Incidentally, pandas happens to use both xlrd and NumPy.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.