Ask Your Question
0

import data .xls

asked 2011-05-06 14:43:33 +0200

massimiliano manfren gravatar image

updated 2011-05-06 15:30:12 +0200

benjaminfjones gravatar image

Hi I would like to run this python script in sage (simple example):

def import_xls(filename, worksheet=1):
    import xlrd
    book = xlrd.open_workbook(filename)
    sh = book.sheet_by_index(worksheet - 1)     
    data = []
    for i in range(sh.ncols):
        data.append(sh.col_values(i))
    print 'Worksheet Names: %s; Rows: %s; Columns: %s'%(sh.name, sh.nrows, sh.ncols)
    return data

def export_xls(data, filename):
    import xlwt
    wb = xlwt.Workbook()
    ws = wb.add_sheet('Sheet1')
    for i in range(len(data)):
        for k in range(len(data[i])):
            ws.write(i,k,str(data[i][k]))
    return wb.save(filename)

from cvxopt import matrix
from cvxopt import solvers
from numpy import array
from numpy import zeros

A = import_xls('sample.xls', worksheet=1)
b = import_xls('sample.xls', worksheet=2)
c = import_xls('sample.xls', worksheet=3)

# linear programming model 
A = matrix(A)  
b = matrix(b)
c = matrix(c)
sol=solvers.lp(c,A,b) 
print sol['x']
x = sol['x']

But it returns the following error: ImportError: No module named xlrd Can you help me? Thanks a lot

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2011-05-06 15:51:27 +0200

benjaminfjones gravatar image

updated 2011-05-06 19:43:14 +0200

The xlrd and xlwt python modules are not included in Sage's python, so you'll have to install them. Download the modules (xlrd is here) in .tar.gz form and unpack in a directory:

~/Downloads> tar -xvzf xlrd-0.7.1.tar.gz
~/Downloads> cd xlrd-0.7.1

Now, have Sage's python install the module:

~/Downloads/xlrd-0.7.1> mysage -python setup.py install

where "mysage" refers to your sage script, for example "/Users/bob/sage-4.6.1/sage". In Sage you can now import and use xlrd.

edit flag offensive delete link more

Comments

I think it is recommended to prepend short options with a single hyphen "-". I'm referring to `tar` in this case.

Kelvin Li gravatar imageKelvin Li ( 2011-05-06 19:40:33 +0200 )edit

right you are!

benjaminfjones gravatar imagebenjaminfjones ( 2011-05-06 19:41:58 +0200 )edit

Although tar x and tar c accept - for compatibility, they aren't actually options, they are required parameters, and have to be first, unlike options in other commands which often are position independent.

jrrk gravatar imagejrrk ( 2023-10-22 15:11:16 +0200 )edit
0

answered 2011-05-06 15:31:30 +0200

kcrisman gravatar image

You'll have to get the xlrd Python module; it's not standard, and has to be installed. It looks like there is a lot of stuff about it out there, though, so hopefully installing it on your system wouldn't be too hard. You should be able to do the setup.py install or whatever in the Sage Python as well, though apparently using easy_install inside the Sage prompt doesn't work for me.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-05-06 14:43:33 +0200

Seen: 3,078 times

Last updated: May 06 '11