Sung Yeon Park
Sung Yeon Park

Reputation: 93

I get the error saying "TypeError: expected string or bytes-like object" how do I fix this?

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

The above is part of my codes. I get the error saying TypeError: expected string or bytes-like object. from datatocopy = final_keys[word]. Any help?

Upvotes: 0

Views: 152

Answers (1)

MockinJay
MockinJay

Reputation: 78

I checked your code and the line you mention throws errors works fine. Here is what I tried :-

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

And here is the output :

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,

Upvotes: 1

Related Questions