python - 我收到错误消息“TypeError:预期的字符串或类似字节的对象”我该如何解决?
问题描述
words = ['apple','bear','cinema']
dataset2 = ['apple','apple','bear','bear','pooh','cinema','cinema']
final_keys = {'apple':'a,b,c,d,b','bear':'s,q,d,f,d,s,d', 'cinema':'a,q,v,d,s,'}
for word in words:
for i in range(len(dataset2)):
if word == str(dataset2[i]):
datatocopy = final_keys[word]
# above is where I get the error from
load_ws[i+1,4] = str(datatocopy)
else:
continue
以上是我的代码的一部分。我TypeError: expected string or bytes-like object.
从datatocopy = final_keys[word]
. 有什么帮助吗?
解决方案
我检查了您的代码,您提到的抛出错误的行工作正常。这是我尝试过的:-
words = ['apple','bear','cinema']
dataset2 = ['apple','apple','bear','bear','pooh','cinema','cinema']
final_keys = {'apple':'a,b,c,d,b','bear':'s,q,d,f,d,s,d', 'cinema':'a,q,v,d,s,'}
for word in words:
for i in range(len(dataset2)):
if word == str(dataset2[i]):
datatocopy = final_keys[word]
print("Values : ", datatocopy)
# above is where I get the error from
# load_ws[i+1,4] = str(datatocopy)
else:
continue
这是输出:
Values : a,b,c,d,b
Values : a,b,c,d,b
Values : s,q,d,f,d,s,d
Values : s,q,d,f,d,s,d
Values : a,q,v,d,s,
Values : a,q,v,d,s,