我正在尝试从文本文件中删除很多东西来重写它。文本文件有数百个项目,每个项目由 6 行组成。我让我的代码工作到将所有行放入数组中,识别每个项目中唯一重要的 2 个并删除空格,但任何进一步的剥离都会给我以下错误:
'list' 对象没有属性 '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
名称的声明中,删除第一个#
会给我错误。我尝试将列表项转换为字符串,但随后我得到了额外[]
的字符串。
for的if
声明color
可以忽略,我知道它有问题,试图解决这个问题是让我遇到当前问题的原因。