简体   繁体   中英

Python 2.6: encode() takes no keyword arguments

Hopefully simple question here, I have a value that based on if its unicode, must be encoded. I use the built in string.encode class

code is simple:

if value_t is unicode:
                values += (value.encode('utf-8', errors='backslashreplace'), None)
                continue

However it returns "encode() takes no keyword arguments"

I am running this in python 2.6, I couldn't find any documentation saying this didn't exist in 2.6 Is there a way for me to make sure it isn't being overwritten by an encode function in a different library? or some sort of solution to it.

It seems like you are able to use string.encode in 2.6 ( https://docs.python.org/2.6/howto/unicode.html ) so I am not really sure why it wouldn't be working. I am working on one file in a fairly big system, so I am worried this is somehow being overwritten. Either that or some module I need isn't installed..But i am lost

The Python docs for encode explain why you're seeing this issue. Specifically: Changed in version 2.7: Support for keyword arguments added

Since method signatures tend to change from version to version, You should always read the relevant documentation to version you are working with

From str.encode documentation for python 2.6, the method signature is:

str.encode([encoding[, errors]])

There is no errors keyword argument, but the second parameter can be used for the same purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM