【发布时间】:2017-11-20 09:52:07
【问题描述】:
我正在尝试从文本文件中删除很多内容以重写它。 文本文件有数百个项目,每个项目由 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 语句中,删除第一个# 会给我错误。
我尝试将列表项转换为字符串,但随后我在字符串周围得到了额外的[]。
color 的 if 语句可以忽略,我知道它有问题,试图解决这个问题是我遇到当前问题的原因。
【问题讨论】:
标签: python arrays file strip multiple-arguments