AttributeError: 'NoneType' object has no attribute 'strip'..while editing CSV

AttributeError: 'NoneType' object has no attribute 'strip'..while editing CSV

考虑以下代码:

import xlwings as xw 
directory("C:\Users\Ritesh\PycharmProjects\BS\Test1.csv") 
wb = xw.Book(directory) 
sht = wb.sheets['Test1'] 
count = 1 
for row in range(2, 200):
    A = 'A%s' % row
    B = 'B%s' % row
    C = 'C%s' % row
    D = 'D%s' % row
    rays = sht.range(A).value
    line = rays.strip().strip(" ")
    code = line.split(" ")[0]
    sht.range('D1').value = 'Code'
    sht.range(D).value = code

请帮我解决这个问题,因为它显示如下错误:

"AttributeError: 'NoneType' object has no attribute 'strip'"

首先,这里是这一行

directory("C:\Users\Ritesh\PycharmProjects\BS\Test1.csv")

perhaps 看起来像一个有效的 Python 语法,但它不应该有效,因为那是一个以 x 作为参数的函数调用 directory(x)。它在 Python2 中无效,在 Python3 中无效,因为在该行之前没有声明函数 directory。因此,按照该逻辑,您的错误不会是 AttributeError,因为错误发生在到达行 wb = xw.Book(directory) 之前。请编辑并包含完整的异常错误。

其次,我只是在 xlwings 文档中进行了快速搜索,但在其中找不到任何函数 directorydirectory 是否有您未包含在此处发布的代码中的某些功能?请仔细检查是否是不小心遗漏的剪切+粘贴问题,或者解释一下您是如何获得该目录功能的。

ps:你的路径看起来也不对,在 windows 中的 iirc 路径将是 C:\,带有单个反斜杠而不是双斜杠,但这可能仍然有效,我的回忆windows 很模糊。