递增函数参数variables
我正在尝试编写一个从Excel文件读取的脚本作为模板,然后根据从.txt文件读取的内容插入新的数据。
我正在使用xlrd
模块进行阅读。 目前,我被困在脚本的阅读部分。
我打算每次将colx
和colx
参数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
你不使用 colx
和colx
variables。 你总是通过0:
if sh.cell_value(rowx=0, colx=0) == cellValue:
代之以传递你的variables的值:
if sh.cell_value(rowx=rowx, colx=colx) == cellValue:
当你在这里,你可以做一些其他的简化。 colx
和colx
总是和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
如果colx
和colx
不是总是相同的,那么你需要修复这个问题,否则你只会沿着主对angular线走。