我想在XL中打开工作表。该工作表可以被命名为“地图”,“地图”或“地图”嵌套try语句打开XL表
这是我在做什么
import xlrd
book = xlrd.open_workbook(xls) // where xls is the name of the spreadsheet
try:
sheet = book.sheet_by_name('map')
except:
try:
sheet = book.sheet_by_name('Map')
except:
try:
sheet = book.sheet_by_name('MAP')
except:
raise
这看起来很笨重......有没有这样做的更pythonic的方法
也有可能有多个名字可能起作用,例如可能存在Map和MAP。迭代方法可让您决定覆盖哪个名称。使用'[0]'只需按照'set'代码选择的顺序选择它们,不一定是人们想要的。如果你有一长串可能的名字,'set'方法会更快,并且在其他情况下很好记住。 :-) – torek 2012-04-11 01:33:14
@torek:尝试用2或3个这样的名字在excel中创建工作簿...它不会让你。 – 2012-04-11 23:23:58
-1完全在顶部。看到我的答案。 – 2012-04-11 23:40:19