当到达 python 中 excel 文件的末尾时退出 while 循环
Exit while loop when it reaches the end of the excel file in python
嗨,我是 python 的新手,正在尝试一些代码来简化我的工作。所以这是我的代码
counter = 0
while True:
row_values = worksheet.row_slice(counter+1,start_colx=0, end_colx=4)
row['Dealer'] = int(row_values[0].value)
row['Name'] = str(row_values[1].value)
row['City'] = str(row_values[2].value)
counter += 1
if not row['Dealer']:
break
我试图在 row['Dealer'] 为空时打破 while 循环,即到达 excel 文件末尾时。但它似乎并没有以某种方式工作。它不断给出 IndexError: list index out of range。一些帮助将不胜感激。谢谢
您好,我将代码编辑为以下内容:
counter = 0
while True:
row_values = worksheet.row_slice(counter+1,start_colx=0, end_colx=30)
row['gg'] = worksheet.cell_value(1+counter,1).strip()
if not row['gg']:
break
row['Dealer'] = int(row_values[0].value)
row['Name'] = str(row_values[1].value)
row['City'] = str(row_values[2].value)
counter += 1
但是错误还是一样!将不胜感激。
pandas 库是使用 Excel 的绝佳选择。
使用 pip 安装 pandas。
pip install pandas
安装 xlrd(处理 Excel 个文件需要)。
pip install xlrd
您可以从那里阅读包含以下内容的 excel 电子表格。
import pandas as pd
df = pd.read_excel('your_excel.xlsx')
df 现在是一个 DataFrame 对象。您可以使用可用的 pandas 工具分析和转换数据。
read_excel().
的完整文档
Question: break the while loop when row['Dealer'] is null, that is when the end of excel file is reached.
您可以使用 worksheet.nrows
获取行尾,例如:
import xlrd
book = xlrd.open_workbook("myfile.xls")
sh = book.sheet_by_index(0)
print("Sheetname:{0} Rows:{1} Columns:{2}".
format(sh.name, sh.nrows, sh.ncols))
# Iterate all Rows
row_value = {}
for rx in range(sh.nrows):
print(sh.row(rx))
row_value['Dealer'] = sh.cell_value(rowx=rx, colx=0)
row_value['Name'] = sh.cell_value(rowx=rx, colx=1)
row_value['City'] = sh.cell_value(rowx=rx, colx=2)
# Break before end of Rows at first Empty Cell
if row_value['Dealer'] == '':
break
嗨,我是 python 的新手,正在尝试一些代码来简化我的工作。所以这是我的代码
counter = 0
while True:
row_values = worksheet.row_slice(counter+1,start_colx=0, end_colx=4)
row['Dealer'] = int(row_values[0].value)
row['Name'] = str(row_values[1].value)
row['City'] = str(row_values[2].value)
counter += 1
if not row['Dealer']:
break
我试图在 row['Dealer'] 为空时打破 while 循环,即到达 excel 文件末尾时。但它似乎并没有以某种方式工作。它不断给出 IndexError: list index out of range。一些帮助将不胜感激。谢谢
您好,我将代码编辑为以下内容:
counter = 0
while True:
row_values = worksheet.row_slice(counter+1,start_colx=0, end_colx=30)
row['gg'] = worksheet.cell_value(1+counter,1).strip()
if not row['gg']:
break
row['Dealer'] = int(row_values[0].value)
row['Name'] = str(row_values[1].value)
row['City'] = str(row_values[2].value)
counter += 1
但是错误还是一样!将不胜感激。
pandas 库是使用 Excel 的绝佳选择。
使用 pip 安装 pandas。
pip install pandas
安装 xlrd(处理 Excel 个文件需要)。
pip install xlrd
您可以从那里阅读包含以下内容的 excel 电子表格。
import pandas as pd
df = pd.read_excel('your_excel.xlsx')
df 现在是一个 DataFrame 对象。您可以使用可用的 pandas 工具分析和转换数据。 read_excel().
的完整文档Question: break the while loop when row['Dealer'] is null, that is when the end of excel file is reached.
您可以使用 worksheet.nrows
获取行尾,例如:
import xlrd
book = xlrd.open_workbook("myfile.xls")
sh = book.sheet_by_index(0)
print("Sheetname:{0} Rows:{1} Columns:{2}".
format(sh.name, sh.nrows, sh.ncols))
# Iterate all Rows
row_value = {}
for rx in range(sh.nrows):
print(sh.row(rx))
row_value['Dealer'] = sh.cell_value(rowx=rx, colx=0)
row_value['Name'] = sh.cell_value(rowx=rx, colx=1)
row_value['City'] = sh.cell_value(rowx=rx, colx=2)
# Break before end of Rows at first Empty Cell
if row_value['Dealer'] == '':
break