【问题标题】:AttributeError: 'NoneType' object has no attribute 'strip'..while editing CSVAttributeError:“NoneType”对象没有属性“strip”..编辑 CSV 时
【发布时间】:2018-09-20 22:40:04
【问题描述】:

考虑以下代码:

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' 对象没有属性 'strip'"

【问题讨论】:

  • 您好。您可能想先自己调试问题。结帐ipdb。在这种情况下,raysrays.strip() 为无。

标签: python csv xlwings


【解决方案1】:

首先,这里是这一行

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

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

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

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

【讨论】:

    猜你喜欢
    • 2017-05-03
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 2020-03-25
    • 2016-08-11
    相关资源
    最近更新 更多