递增函数参数variables

我正在尝试编写一个从Excel文件读取的脚本作为模板,然后根据从.txt文件读取的内容插入新的数据。

我正在使用xlrd模块进行阅读。 目前,我被困在脚本的阅读部分。

我打算每次将colxcolx参数variables加1,以便在Excel文件中search每个单元格。 但是,似乎Python的参数variables不能被修改?

我的脚本增量修改外部variables。 有没有办法实现逐个单元格的值search?

 # Define variables switch1 = 1 count = 0 rowx = 0 colx = 0 # Loop while switch1 == 1 and count < totalCells: print "AAAA" cellValue = "Long string in here....." if sh.cell_value(rowx=0, colx=0) == cellValue: print "BBBB" switch1 = 0 rowx += 1 colx += 1 count += 1 

你不使用 colxcolxvariables。 你总是通过0:

 if sh.cell_value(rowx=0, colx=0) == cellValue: 

代之以传递你的variables的值:

 if sh.cell_value(rowx=rowx, colx=colx) == cellValue: 

当你在这里,你可以做一些其他的简化。 colxcolx总是和count相同,所以你可以只使用一个variables。 我们可以使用for循环从0循环到totalCells-1 ,并使用break语句尽早结束循环:

 for i in xrange(totalCells): print "AAAA" cellValue = "Long string in here....." if sh.cell_value(rowx=i, colx=i) == cellValue: print "BBBB" break 

如果colxcolx不是总是相同的,那么你需要修复这个问题,否则你只会沿着主对angular线走。