So your new code looks like this?
WORDS.append(str(word.strip())
If that’s true then you should NOT get that error that says:
TypeError: append() takes no keyword arguments
This error is saying you did this:
WORDS.append(word.strip(), encoding="utf-8")
If you break this down you can see the problem. Let’s turn everything into variables:
stripped_word = word.strip()
utf8_encoded_word = str(stripped_word, encoding="utf-8")
WORDS.append(utf8_encoded_word)
Now, that’s the correct code but “exploded out” into multiple variables so you can see each step. Any time you can’t understand a chain like this turn it into a bunch of variables to see each step. Next, if you’re still getting that error then you probably did this:
stripped_word = word.strip()
utf8_encoded_word = str(stripped_word)
WORDS.append(utf8_encoded_word, encoding="utf-8")
See how there’s the encoding="utf-8"
in the append call? But in my code right above this that encoding="utf-8"
is in the str call where I make the variable utf8_encoded_word
.
That’s the first thing to figure out in your confusion so, paste me back your snippet of code where you get this error.
The reason I’m hammering on this confusion is you think I just did this:
x = str(y)
But I didn’t, I did:
x = str(y, encoding="utf-8")
I think your confusion is because you think the encoding="utf-8"
belongs to the append and not the str. That’s why you see it doing nothing and also why you get that error.
The reason I did str(y, encoding="utf-8")
is probably because I kept running into people with computers that are configured for non-utf8 characters, like Big5. In those cases their computer will save text files in Big5, and then Python throws a little fit because it wants everything to be utf-8. Your computer probably is set to either utf-8 or an English language character set so you don’t need it.
But, the most important thing is for you to figure out that error after you change it.