2
votes

When reading about codecs, encoding and decoding I found that i should use the encode function on the string directly and that worked fine. I've after that read about what the unicode and ascii is in addition to the different utf encodings.

But when reading further i found that most people seem to import the codecs module and use encode from the module. I dont see much of a difference between String.encode and codecs.encode.. does it matter which one i use ? I'm just specifying the encoding i need in the encode function.

Also, when reading this thread python string encode / decode i looked at the link in the accepted answer which shows a slide show which is suppose to "completely demystify unicode and utf" but on one of the slides he says that utf is used to translate numbers to characters which i cant see is correct.

From my understanding based on http://www.rrn.dk/the-difference-between-utf-8-and-unicode which was also quoted in another SO thread utf is not translating numbers to characters. Its translating binary numbers to numbers found in the unicode or the other choosen character set being used. So utf would be translation of a binary number to a number and then unicode would be translating that number again to a character..so he got it wrong when trying to completely mystify this?

3
Do you mean str.encode()? - KSFT
@KSFT yes it should be str.encode() and not String.encode - exceed
Did you see my answer? - KSFT
Yes, i guess they are pretty much the same but seems like codecs are more flexible as it can take an object..it doesnt seem to be clear from the docs but maybe it also can do other types of encoding than just text as well mhm. I didnt reply to your answer because i didnt really see that it offered an answer to the question as there probably should be a reason for it to have two different functions but i guess since codecs.encode can take an object its more flexible - exceed

3 Answers

1
votes

The Python doc pages for these two functions are here:

https://docs.python.org/2/library/stdtypes.html#str.encode

https://docs.python.org/2/library/codecs.html#codecs.encode


str.encode() is called on a string object like this:

"this is a string".encode()

codecs.encode() is called with a string as an argument like this:

codecs.encode("this is a string")

They each take an optional encoding argument.

str.encode()'s default encoding is the current default, according to the doc page, but according the the Unicode HOWTO, that's "ascii"

codecs.encode()'s default encoding is "ascii"


Both functions take an errors argument that defaults to "strict".


It looks like they're pretty much the same except for the way they're called.

1
votes
  1. codecs.encode(obj, encoding='utf-8', errors='strict')

encode text to bytes, text to text, and bytes to bytes

  1. str.encode(encoding="utf-8", errors="strict")

encode text to bytes

so, I think 2.⊆1.

0
votes

One difference is what codecs you can use. str.encode is fine for casting among string codecs, but try converting a string to base64.

str.encode("base64")
LookupError: 'base64' is not a text encoding; use codecs.encode() to handle arbitrary codecs

but this will work

codecs.encode(str.encode(), "base64")

or this

base64.encodestring(str.encode())