First time here? Check out the FAQ!

Ask Your Question
0

import data .xls

asked 13 years ago

massimiliano manfren gravatar image

updated 13 years ago

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

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
2

answered 13 years ago

benjaminfjones gravatar image

updated 13 years ago

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.

Preview: (hide)
link

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 ( 13 years ago )

right you are!

benjaminfjones gravatar imagebenjaminfjones ( 13 years ago )

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 ( 1 year ago )
0

answered 13 years ago

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.

Preview: (hide)
link

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: 13 years ago

Seen: 3,249 times

Last updated: May 06 '11