Python/xrld 读取行和列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14944623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:55:21  来源:igfitidea点击:

Python/xrld Read rows and columns

pythonxlrd

提问by Protoss Reed

I have exel file with specific structure. There are first row is title second are names and the last one are values. I need take only title and values it is not so difficult if Excel file has only 3 rows, but it can be 100 rows and columms and i need to get only Title and values, not any names and empty rows

我有特定结构的exel文件。第一行是标题,第二行是名称,最后一行是值。我只需要获取标题和值,如果 Excel 文件只有 3 行,这并不难,但它可以是 100 行和列,我只需要获取标题和值,而不是任何名称和空行

         1          2        3           
1    GroupOne     Empty      Empty
2      Jonh       Elena     Mike    
3        45         100      500 
4        Empty      Empty     Empty
5   GroupTwo       Empty     Empty
6   Lisa           Ken       Lui
7   100             300      400

And etc.

等等。

As You see between two Title always is one Empty row. Finaly it will be something like this:

如您所见,两个标题之间始终是一个空行。最后它会是这样的:

GroupOne
45   100   500
GroupTwo
100 300 400

Thank you in advance. I would be so grateful for some help

先感谢您。我会很感激一些帮助

采纳答案by Schorsch

I think this websitehas an example that will help you:

我认为这个网站有一个例子可以帮助你:

import xlrd
workbook = xlrd.open_workbook('my_workbook.xls')
worksheet = workbook.sheet_by_name('Sheet1')
num_rows = worksheet.nrows - 1
num_cells = worksheet.ncols - 1
curr_row = -1
while curr_row < num_rows:
  curr_row += 1
  row = worksheet.row(curr_row)
  print 'Row:', curr_row
  curr_cell = -1
  while curr_cell < num_cells:
    curr_cell += 1
    # Cell Types: 0=Empty, 1=Text, 2=Number, 3=Date, 4=Boolean, 5=Error, 6=Blank
    cell_type = worksheet.cell_type(curr_row, curr_cell)
    cell_value = worksheet.cell_value(curr_row, curr_cell)
    print ' ', cell_type, ':', cell_value

The function cell_typeshould help you build a if-statement such as if worksheet.cell_type != 0and thus skip empty cells.

该函数cell_type应该帮助您构建一个 if 语句,例如if worksheet.cell_type != 0并因此跳过空单元格。