

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between encode/decode in Python?
To represent a unicode string as a string of bytes is known as encoding. To convert a string of bytes to a unicode string is known as decoding. You typically encode a unicode string whenever you need to use it for IO, for instance transfer it over the network, or save it to a disk file. You typically decode a string of bytes whenever you receive string data from the network or from a disk file.
To encode a string using a given encoding you can do the following:
>>>u'æøå'.encode('utf8') '\xc3\x83\xc2\xa6\xc3\x83\xc2\xb8\xc3\x83\xc2\xa5'
To decode astring(using the same encoding used to encode it), you need to call decode(encoding). For example:
>>>'\xc3\x83\xc2\xa6\xc3\x83\xc2\xb8\xc3\x83\xc2\xa5'.decode('utf8') u'\xc3\xa6\xc3\xb8\xc3\xa5'
This string in utf8 encoding is equivalent to u'æøå'
- Related Questions & Answers
- Encode and decode uuencode files using Python
- Encode and Decode Strings in C++
- Encode and decode binhex4 files using Python (binhex)
- Encode and decode XDR data using Python xdrlib
- Arduino – base64 encode and decode
- Encode and decode MIME quoted-printable data using Python
- How to encode and decode a URL in JavaScript?
- How to Encode and Decode JSON and Lua Programming?
- What is the difference between = and == operators in Python?
- What is the difference between attributes and properties in python?
- What is the difference between os.open and os.fdopen in python?
- What is the difference between dict.items() and dict.iteritems() in Python?
- What is the difference between __str__ and __repr__ in Python?
- What is the difference between semicolons in JavaScript and in Python?
- What is the difference between Python functions datetime.now() and datetime.today()?
Advertisements