Search code examples
pythonmatriximport-from-excel

reading entries from workbook sheet in python


In the code attached, I have a matrix as below:
guyprefers = {
'bob': ['abi', 'dee', 'fay', 'bea', 'jan'],
'gav': ['abi', 'bea', 'dee', 'jan', 'fay'],
'ian': ['bea', 'dee', 'abi', 'fay', 'jan'],
'jon': ['bea', 'abi', 'fay', 'jan', 'dee']}

I want to read above matrix from a worksheet preferences.xls preferences.xls looks like below: bob abi dee fay bea jan
gav abi bea dee jan fay
ian bea dee abi fay jan
jon bea abi fay jan dee

and subsequently produce results. I tried to replace the matrix row 1 as below:
'worksheet.cell(0, 0).value': ['worksheet.cell(0, 1).value, 'worksheet.cell(0, 2).value', 'worksheet.cell(0, 3).value', 'worksheet.cell(0, 4).value', 'worksheet.cell(0, 5).value'],
But it showing error. Where the code went wrong?

import copy
import xlrd
import xlwt
workbook = xlrd.open_workbook('preferences.xls')
worksheet = workbook.sheet_by_name('guys')

guyprefers = {
'bob': ['abi', 'dee', 'fay', 'bea', 'jan'],
'gav': ['abi', 'bea', 'dee', 'jan', 'fay'],
'ian': ['bea', 'dee', 'abi', 'fay', 'jan'],
'jon': ['bea', 'abi', 'fay', 'jan', 'dee']}
galprefers = {
'abi': ['gav', 'bob', 'jon', 'ian'],
'bea': ['bob', 'gav', 'jon', 'ian'],
'dee': ['jon', 'ian', 'gav', 'bob'],
'fay': ['bob', 'ian', 'jon', 'gav'],
'jan': ['gav', 'bob', 'jon', 'ian']}
 
guys = sorted(guyprefers.keys())
gals = sorted(galprefers.keys())
 
 
def check(engaged):
    inverseengaged = dict((v,k) for k,v in engaged.items())
    for she, he in engaged.items():
        shelikes = galprefers[she]
        shelikesbetter = shelikes[:shelikes.index(he)]
        helikes = guyprefers[he]
        helikesbetter = helikes[:helikes.index(she)]
        for guy in shelikesbetter:
            guysgirl = inverseengaged[guy]
            guylikes = guyprefers[guy]
            if guylikes.index(guysgirl) > guylikes.index(she):
                print("%s and %s like each other better than "
                      "their present partners: %s and %s, respectively"
                      % (she, guy, he, guysgirl))
                return False
        for gal in helikesbetter:
            girlsguy = engaged[gal]
            gallikes = galprefers[gal]
            if gallikes.index(girlsguy) > gallikes.index(he):
                print("%s and %s like each other better than "
                      "their present partners: %s and %s, respectively"
                      % (he, gal, she, girlsguy))
                return False
    return True
 
def matchmaker():
    guysfree = guys[:]
    engaged  = {}
    guyprefers2 = copy.deepcopy(guyprefers)
    galprefers2 = copy.deepcopy(galprefers)
    while guysfree:
        guy = guysfree.pop(0)
        guyslist = guyprefers2[guy]
        gal = guyslist.pop(0)
        fiance = engaged.get(gal)
        if not fiance:
            # She's free
            engaged[gal] = guy
            print("  %s and %s" % (guy, gal))
        else:
            # The bounder proposes to an engaged lass!
            galslist = galprefers2[gal]
            if galslist.index(fiance) > galslist.index(guy):
                # She prefers new guy
                engaged[gal] = guy
                print("  %s dumped %s for %s" % (gal, fiance, guy))
                if guyprefers2[fiance]:
                    # Ex has more girls to try
                    guysfree.append(fiance)
            else:
                # She is faithful to old fiance
                if guyslist:
                    # Look again
                    guysfree.append(guy)
    return engaged
 
 
print('\nEngagements:')
engaged = matchmaker()
 
print('\nCouples:')
print('  ' + ',\n  '.join('%s is engaged to %s' % couple
                          for couple in sorted(engaged.items())))
print()
print('Engagement stability check PASSED'
      if check(engaged) else 'Engagement stability check FAILED')
 


Solution

  • I believe you are trying to retrieve the value "bob" by using 'worksheet.cell(0, 0).value'.

    However, because of your quotation marks, 'worksheet.cell(0, 0).value' is interpreted as a string literal, and does not resolve to "bob". If you remove your single quotation marks, it will work the way you want.

    For example, the below code can replace what you have for guyprefers:

    guyprefers = {
    worksheet.cell(0, 0).value: [worksheet.cell(0, 1).value, worksheet.cell(0, 2).value, worksheet.cell(0, 3).value, worksheet.cell(0, 4).value, worksheet.cell(0, 5).value],
    worksheet.cell(1, 0).value: [worksheet.cell(1, 1).value, worksheet.cell(1, 2).value, worksheet.cell(1, 3).value, worksheet.cell(1, 4).value, worksheet.cell(1, 5).value],
    worksheet.cell(2, 0).value: [worksheet.cell(2, 1).value, worksheet.cell(2, 2).value, worksheet.cell(2, 3).value, worksheet.cell(2, 4).value, worksheet.cell(2, 5).value],
    worksheet.cell(3, 0).value: [worksheet.cell(3, 1).value, worksheet.cell(3, 2).value, worksheet.cell(3, 3).value, worksheet.cell(3, 4).value, worksheet.cell(3, 5).value]}
    

    That should get you started. Note that it might be easier to do it with a for loop.