Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用pywin32从Excel工作表读取和分析数据_Python_Excel_Pywin32 - Fatal编程技术网

Python 使用pywin32从Excel工作表读取和分析数据

Python 使用pywin32从Excel工作表读取和分析数据,python,excel,pywin32,Python,Excel,Pywin32,我已经看过很多关于如何编写excel的例子,我的程序需要从现有数据中读取数据。我需要通读一列并从中选择数字(该列为空单元格,以任意间隔显示数字),然后在到达数据末尾时中断。下面是我的代码,其中xlsht是表名,x和y是单元格索引,address是所有数字的列表 while y < xlsht.UsedRange: if str(xlsht.Cells(x,y).Value).isdigit: address.append(xlsht.Cells(x,y).Value

我已经看过很多关于如何编写excel的例子,我的程序需要从现有数据中读取数据。我需要通读一列并从中选择数字(该列为空单元格,以任意间隔显示数字),然后在到达数据末尾时中断。下面是我的代码,其中xlsht是表名,x和y是单元格索引,address是所有数字的列表

while y < xlsht.UsedRange:
    if str(xlsht.Cells(x,y).Value).isdigit:
        address.append(xlsht.Cells(x,y).Value)
        y += 1
        continue
    else:
        y += 1
return address
但那没用。我不知道还能怎么做。此外,我还想验证使用

xlsht.UsedRange 

是检测工作表最后一行的正确方法。

错误消息没有提供任何有用的线索,说明问题可能是什么

解决方案:可怜人的调试器。打印所有和任何变量,以便查看发生的情况:

print 'xlsht=',xlsht
print 'x=',x
print 'usedRange=',xlsht.UsedRange
while y < xlsht.UsedRange:
    print 'y=',y
    print 'cell=',xlsht.Cells(x,y)
    print 'value=',xlsht.Cells(x,y).Value
    if str(xlsht.Cells(x,y).Value).isdigit:
        address.append(xlsht.Cells(x,y).Value)
        y += 1
        continue
    else:
        y += 1
return address
打印“xlsht=”,xlsht
打印“x=”,x
打印“usedRange=”,xlsht.usedRange
当y