Python提取excel单元格

我正在读取数百个excel文件并search一个string,然后是该string旁边的值。 我面临的问题是string很less在同一个单元格中。 举个例子:

表1

Name Amount foo 15 bar 23 bin 10 

工作表2

  Name Amount bin 28 foo 10 bar 6 

我想读取文件,并写入| foo | #| 在另一个excel表中,每个实例都是一个新行。

我正在努力采取一种方法。

期望的输出

  Name Amount foo 15 foo 10 

 from operator import itemgetter #use zero based columns name_col=0 value_col=1 #make a big dict with all the name value pairs ... this might come in handy later foo=dict([itemgetter(name_col,val_col)(sheet.get_row_values(i))for i in range(nRows)]).get('foo',None) print "FOO:",foo 

像我认为会起作用的东西…如果你真的不关心所有值的字典,你可能只是做类似的事情

 def get_foo(sheet,nRows): for i in range(nRows): values = sheet.get_row_values() if 'foo' in values: return values[values.index('foo')+1] 

一旦你find自己的价值,你就可以在这里纾困