xlrd。一次打印一行。难以增加行数

时间:2019-06-15 17:57:18

标签: python

我正在尝试编写一个脚本,一次可提取一行Excel并将其打印出来。我想使用一种方法来更改行。我能够获取要更改的行的值(变量rrowx),但是当我打印currentRow字符串时,我得到的是原始行,而不是新调整的行。

import xlrd
class Loader(object): ## engine to load and unload spread sheets 
                      ## then sets them to a variable
     # set the variables
     workbook = " "      # name of the file
     sheetCount = 0      # amount of sheets in the spreadsheet
     sheetNumber = 0     # current sheet (index)
     rowCount = 0        # amount of rows in the spreadsheet
     currentSheet = " "  # name of current sheet
     topRow = " "        # row 0 string
     currentRow = " "    # row x string
     global rrowx
     rrowx = 0

     # begin the load  
     workbook = xlrd.open_workbook('test.xlsx')
     sheetCount = workbook.nsheets
     sheetNames = workbook.sheet_names()
     currentSheet = workbook.sheet_by_index(sheetNumber)
     #topRow = currentSheet.row_values(rowx=rrowx, start_colx=scolx, end_colx=ecolx)
     currentRow = currentSheet.row_values(rowx=rrowx)

    #  methods to navigate the sheet 
     def nextrow(self):
        global rrowx
        print(rrowx)
        rrowx += 1 
        print(rrowx)
        return rrowx

spreadsheet = Loader()
## Debuggin prints
print(spreadsheet.sheetNames)
print(spreadsheet.sheetCount)
print("What Sheet would you like to use? (Use numbers)")
spreadsheetadjust = int(input())                   # takes input as a interger
spreadsheet.currentSheet = spreadsheetadjust - 1   # takes input and -1 for index value
print ('Current sheet name: %s' % spreadsheet.currentSheet)# prints current sheet name
print('top row:')
#print(spreadsheet.topRow)
print('row 1 ')
print(spreadsheet.currentRow)
print("NextRow")
spreadsheet.nextrow()
print(spreadsheet.currentRow)

我认为在更改rrowx变量并在currentRow上再次调用print之后,将更改要打印的行。但是,即使我看到rrowx的值随着我在方法中添加的打印而改变,但我却将同一行打印了两次。

披露:我只编程了一个月,所以很抱歉,我只是想念这个简单的答案。

1 个答案:

答案 0 :(得分:0)

我强烈建议您阅读python面向对象的基础知识。您的代码有多个问题,我会提到一些问题:

  • 您的类变量是类变量,表示的所有实例 该类将共享相同的变量。因此,如果您创建多个 您的类实例,您将获得意外/不良行为。

  • 不建议使用global变量,尤其是当您 可以没有它。

  • 您不需要在python中初始化变量

  • 在您的实现中,您有 spreadsheet.currentSheet = spreadsheetadjust - 1,即使您修复它也将导致您失败 你的问题。你要 spreadsheet.sheetNumber = spreadsheetadjust - 1

以下是可正确使用python类的代码:

import xlrd


class Loader:
    def __init__(self, path_to_xlsx='test.xlsx'):
        self.sheetNumber = 0
        self.rrowx = 0
        self.workbook = xlrd.open_workbook(path_to_xlsx)
        self.sheetCount = self.workbook.nsheets
        self.sheetNames = self.workbook.sheet_names()
        self.currentSheet = self.workbook.sheet_by_index(self.sheetNumber)
        self.currentRow = self.currentSheet.row_values(rowx=self.rrowx)

    def nextrow(self):
        self.rrowx += 1
        self.currentRow = self.currentSheet.row_values(rowx=self.rrowx)


spreadsheet = Loader()

print("What Sheet would you like to use? (Use numbers)")
spreadsheetadjust = int(input())  # takes input as a interger
spreadsheet.sheetNumber = spreadsheetadjust - 1  # takes input and -1 for index value

print('Current sheet name: %s' % spreadsheet.currentSheet)  # prints current sheet name
print(spreadsheet.currentRow)

spreadsheet.nextrow()
print(spreadsheet.currentRow)