Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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 3.x 遍历Excel工作表中的列_Python 3.x_Openpyxl - Fatal编程技术网

Python 3.x 遍历Excel工作表中的列

Python 3.x 遍历Excel工作表中的列,python-3.x,openpyxl,Python 3.x,Openpyxl,我正在努力实现这个结果 | method | | passed | | datetime | method | | passed | | datetime | method | | passed | | datetime | method | | passed | | datetime 为了实现这一点,我目前有一个类XLWriter class XLWriter: def __init__(self, file): self.workbook = load_workbo

我正在努力实现这个结果

| method | | passed | | datetime
| method | | passed | | datetime
| method | | passed | | datetime
| method | | passed | | datetime
为了实现这一点,我目前有一个类
XLWriter

class XLWriter:
    def __init__(self, file):
        self.workbook = load_workbook(file)
        self.worksheet = self.workbook['Sheet1']
        self.file = file

    def write(self, row):
        index = 0
        try:
            # func name, test passed, date
            values = [[inspect.stack()[0][3]], True, datetime.date]

            columns = [4, 6, 8]

            # 3 writes to perform
            while index < 2:
                cell = self.worksheet.cell(row=row, column=columns[index])
                cell.value = values[index]
                index += 1
        except:
            # function name, declare test failed, date
            values = [[inspect.stack()[0][3]], False, datetime.date]
            columns = [4, 6, 8]
            while index < 2:
                cell = self.worksheet.cell(row=row, column=columns[index])
                cell.value = values[index]
                index += 1

    def save(self):
        self.workbook.save(self.file)
我遇到了以下错误

ValueError: Cannot convert ['write'] to Excel
我相信这是一件小而愚蠢的事情,非常感谢您的帮助

编辑

完全错误堆栈

Error
Traceback (most recent call last):
  File "D:\...\xlwriter.py", line 21, in write
    cell.value = values[index]
  File "D:\...\env\lib\site-packages\openpyxl\cell\cell.py", line 294, in value
    self._bind_value(value)
  File "D:\...\env\lib\site-packages\openpyxl\cell\cell.py", line 207, in _bind_value
    raise ValueError("Cannot convert {0!r} to Excel".format(value))
ValueError: Cannot convert ['write'] to Excel

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\...\AppData\Local\Programs\Python\Python37-32\lib\unittest\case.py", line 59, in testPartExecutor
    yield
  File "C:\Users\...\AppData\Local\Programs\Python\Python37-32\lib\unittest\case.py", line 615, in run
    testMethod()
  File "D:\...\test.py", line 40, in test
    xl.write(14)
  File "D:\...\xlwriter.py", line 29, in write
    cell.value = values[index]
  File "D:\...\env\lib\site-packages\openpyxl\cell\cell.py", line 294, in value
    self._bind_value(value)
  File "D:\...\env\lib\site-packages\openpyxl\cell\cell.py", line 207, in _bind_value
    raise ValueError("Cannot convert {0!r} to Excel".format(value))
ValueError: Cannot convert ['write'] to Excel

当您试图将列表写入单元格时,代码失败

请更新代码,使列表“value”的第一个元素为inspect.stack()[0][3],而不是[inspect.stack()[0][3]]

更新代码:

class XLWriter:
    def __init__(self, file):
        self.workbook = load_workbook(file)
        self.worksheet = self.workbook['Sheet1']
        self.file = file

    def write(self, row):
        index = 0
        try:
            # func name, test passed, date
            values = [inspect.stack()[0][3], True, datetime.date]

            columns = [4, 6, 8]

            # 3 writes to perform
            while index < 2:
                cell = self.worksheet.cell(row=row, column=columns[index])
                cell.value = values[index]
                index += 1
        except:
            # function name, declare test failed, date
            values = [inspect.stack()[0][3], False, datetime.date]
            columns = [4, 6, 8]
            while index < 2:
                cell = self.worksheet.cell(row=row, column=columns[index])
                cell.value = values[index]
                index += 1

    def save(self):
        self.workbook.save(self.file)
class-XLWriter:
定义初始化(自我,文件):
self.workbook=加载工作簿(文件)
self.worksheet=self.workbook['Sheet1']
self.file=文件
def写入(自身,行):
索引=0
尝试:
#func名称,测试通过,日期
值=[inspect.stack()[0][3],True,datetime.date]
列=[4,6,8]
#要执行的3次写入
当指数<2时:
cell=self.worksheet.cell(行=行,列=列[索引])
cell.value=值[索引]
指数+=1
除:
#函数名,声明测试失败,日期
值=[inspect.stack()[0][3],False,datetime.date]
列=[4,6,8]
当指数<2时:
cell=self.worksheet.cell(行=行,列=列[索引])
cell.value=值[索引]
指数+=1
def保存(自我):
self.workbook.save(self.file)

在删除您共享的部分代码(与self一起调用)后,您可以共享完整的错误堆栈吗?我可以写入该文件。当然,OP updated@Sach
class XLWriter:
    def __init__(self, file):
        self.workbook = load_workbook(file)
        self.worksheet = self.workbook['Sheet1']
        self.file = file

    def write(self, row):
        index = 0
        try:
            # func name, test passed, date
            values = [inspect.stack()[0][3], True, datetime.date]

            columns = [4, 6, 8]

            # 3 writes to perform
            while index < 2:
                cell = self.worksheet.cell(row=row, column=columns[index])
                cell.value = values[index]
                index += 1
        except:
            # function name, declare test failed, date
            values = [inspect.stack()[0][3], False, datetime.date]
            columns = [4, 6, 8]
            while index < 2:
                cell = self.worksheet.cell(row=row, column=columns[index])
                cell.value = values[index]
                index += 1

    def save(self):
        self.workbook.save(self.file)