gpt4 book ai didi

python - 使用 python 检测空白 Excel 单元格

转载 作者:太空宇宙 更新时间:2023-11-03 14:24:53 27 4
gpt4 key购买 nike

我已经编写了一个 python 代码,用于将数据从多个 excel 文件转换为单个 csv 文件,但是它不起作用。我在以下程序中尝试用 XL_CELL_EMPTY 或 XL_CELL_BLANK 代替 None。但没有任何效果。它给出了以下错误

Traceback (most recent call last):
File "Excel_to_CSV_1.py", line 35, in <module>
while Sh_B1.cell(i,j).value != None :
File "/usr/lib/pymodules/python2.7/xlrd/sheet.py", line 245, in cell
self._cell_types[rowx][colx],
IndexError: array index out of range

这是程序:

import xlrd
import os.path
from xlrd import open_workbook,empty_cell
book1 = xlrd.open_workbook("Excel_1.xls")
Sh_B1 = book1.sheet_by_index(0)
book2 = xlrd.open_workbook("Excel_2.xls")
Sh_B2 = book2.sheet_by_index(0)
i = 1
j = 1
file = open("Output.txt", "w")
while Sh_B1.cell(i,j).value != None :
while Sh_B1.cell(i,j).value != None :
D1 = Sh_B1.cell(i,j).value
D2 = Sh_B2.cell(i,j).value
DB1 = str(D1)+ "," + str(D2)
file.write("".join(DB1+ "\n"))
j = j + 1
j = 1
print "j=1"
i = i + 1
file.close

我哪里错了?

最佳答案

xlrdSheet 对象有一个 nrows 和一个 ncols 属性,可以对单元格进行迭代.

import xlrd
book = xlrd.open_workbook("my_file.xls")
sheet = book.sheet_by_index(0) # or: sheet_by_name(Sheet 1")
for row_index in xrange(sheet.nrows):
for col_index in xrange(sheet.ncols):
value = sheet.cell(rowx=row_index,colx=col_index).value

关于python - 使用 python 检测空白 Excel 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22087768/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com