带多个参数错误的剥离列表项

Strip list items with multiple arguments error

我正在尝试从文本文件中删除大量内容以重写它。 该文本文件有数百个项目,每个项目由 6 行组成。 我让我的代码工作到将所有行放在一个数组中,识别每个项目中唯一重要的 2 行并删除空格,但是任何进一步的剥离都会给我以下错误:

'list' object has no attribute 'strip'

这是我的代码:

x = 0
y = 0
names = []
colors = []
array = []

with open("AA_Ivory.txt", "r") as ins:

    for line in ins:
        array.append(line)

def Function (currentElement, lineInSkinElement):
    name = ""
    color = ""
    string = array[currentElement]
    if lineInSkinElement == 1:
        string = [string.strip()]
#       string = [string.strip()]
#       name = [str.strip("\n")]
#       name = [str.strip(";")]
#       name = [str.strip(" ")]
#       name = [str.strip("=")]
        names.append(name)
        return name
#   if lineInSkinElement == 2:
#       color = [str.strip("\t")]
#       color = [str.strip("\n")]
#       color = [str.strip(";")]
#       color = [str.strip(" ")]
#       color = [str.strip("=")]
#       colors.append(color)
#       return color
    print "I got called %s times" % currentElement
    print lineInSkinElement
    print currentElement

for val in array:
    Function(x, y)
    x = x +1
    y = x % 6

#print names
#print colors

在名称的 if 语句中,删除第一个 # 会给我错误。 我尝试将列表项转换为字符串,但后来我在字符串周围得到了额外的 []

可以忽略 colorif 语句,我知道它有问题,并且试图解决这个问题是我遇到当前问题的原因。

but then I get extra [] around the string

您可以遍历它以绕过列出的字符串。例如:

for lst, item in string:
    item = item.strip("\n")
    item = item.strip(";")
    item = item.strip(" ")
    item = item.strip("=")
    name.append(item)
    return name

这将使您找到列表中的字符串,您可以附加剥离的字符串。

如果这不是您想要的,post 您正在使用的一些数据来澄清。

好的,我找到了解决方案。这是我的一个相当愚蠢的错误。错误发生是由于 [] arroung strip 函数使结果成为列表或列表项。删除它们修复它。现在松了口气,有点傻,但是松了口气

您也可以使用以下代码在一行中执行此操作。

item = item.strip("\n").strip("=").strip(";").strip()

The last strip will strip the white spaces.