Python - AttributeError: 'str' 对象没有属性 'append'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27878301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python - AttributeError: 'str' object has no attribute 'append'
提问by Dstewart334
I keep receiving this error when I try to run this code for the line "encoded.append("i")":
当我尝试为“encoded.append("i")”行运行此代码时,我不断收到此错误:
AttributeError: 'str' object has no attribute 'append'
AttributeError: 'str' 对象没有属性 'append'
I cannot work out why the list won't append with the string. I'm sure the problem is very simple Thank you for your help.
我无法弄清楚为什么列表不会附加字符串。我相信问题很简单 谢谢你的帮助。
def encode(code, msg):
'''Encrypts a message, msg, using the substitutions defined in the
dictionary, code'''
msg = list(msg)
encoded = []
for i in msg:
if i in code.keys():
i = code[i]
encoded.append(i)
else:
encoded.append(i)
encoded = ''.join(encoded)
return encoded
采纳答案by James
>>> encoded =["d","4"]
>>> encoded="".join(encoded)
>>> print (type(encoded))
<class 'str'> #It's not a list anymore, you converted it to string.
>>> encoded =["d","4",4] # 4 here as integer
>>> encoded="".join(encoded)
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
encoded="".join(encoded)
TypeError: sequence item 2: expected str instance, int found
>>>
As you see, your list is converted to a string in here "".join(encoded). And appendis a method of lists, not strings. That's why you got that error. Also as you see if your encodedlist has an element as integer, you will see TypeErrorbecause, you can't use joinmethod on integers. Better you check your all codes again.
如您所见,您的列表在此处转换为字符串"".join(encoded)。Andappend是一种列表方法,而不是字符串。这就是你得到那个错误的原因。同样,当您看到您的encoded列表是否有一个整数元素时,您会看到,TypeError因为您不能join对整数使用方法。最好再次检查所有代码。
回答by Andrey Rusanov
You set encoded to string here:
您在此处将编码设置为字符串:
encoded = ''.join(encoded)
And of course it doesn't have attribute 'append'.
当然,它没有属性“附加”。
Since you're doing it on one of cycle iteration, on next iteration you have str instead of list...
由于您是在一次循环迭代中执行此操作,因此在下一次迭代中,您将使用 str 而不是 list ...
回答by James
Your string conversion line is under the elseclause. Take it out from under the conditional, and the for loop so that it's the last thing done to encoded. As it stands, you are converting to a string halfway through your forloop:
您的字符串转换行在else子句下。从条件和 for 循环中取出它,以便它是对encoded. 就目前而言,您正在for循环中途转换为字符串:
def encode(code, msg):
'''Encrypts a message, msg, using the substitutions defined in the
dictionary, code'''
msg = list(msg)
encoded = []
for i in msg:
if i in code.keys():
i = code[i]
encoded.append(i)
else:
encoded.append(i)
# after all appends and outside for loop
encoded = ''.join(encoded)
return encoded
回答by Loupi
You are getting the error because of the second expression in you else statement.
由于 else 语句中的第二个表达式,您收到错误。
''.join(encoded) returns a string that gets assigned to encoded
Thus encoded is now of type string. In the second loop you have the .append(i) method in either if/else statements which can only be applied to lists and not strings.
因此编码现在是字符串类型。在第二个循环中,您在 if/else 语句中都有 .append(i) 方法,该方法只能应用于列表而不是字符串。
Your .join() method should appear after the for loop right before you return it.
您的 .join() 方法应该在返回之前出现在 for 循环之后。
I apoligise if the above text does not appear right. This is my first post and I still trying to figure out how this works.
如果上述文字显示不正确,我会道歉。这是我的第一篇文章,我仍在努力弄清楚这是如何工作的。

