阅读 python 中工作簿 sheet 中的条目

reading entries from workbook sheet in python

在附带的代码中,我有一个矩阵如下:
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']}

我想从工作表中读取上面的矩阵 preferences.xls preferences.xls 如下所示: bob abi dee fay bea jan
gav abi bea dee jan fay
ian bea dee abi fay jan
jon bea abi fay jan dee

并随后产生结果。我尝试将矩阵第 1 行替换如下:
'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'],
但它显示错误。代码哪里出错了?

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')
 

我相信您正在尝试使用 'worksheet.cell(0, 0).value'.

检索值 "bob"

但是,由于您的引号,'worksheet.cell(0, 0).value' 被解释为字符串文字,不会解析为 "bob"。如果您删除单引号,它将按您希望的方式工作。

例如,下面的代码可以替换 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]}

这应该可以帮助您入门。请注意,使用 for 循环可能更容易。