相关文章推荐
傲视众生的玉米  ·  Netty实战一 ...·  16 小时前    · 
高大的冰棍  ·  高性能TcpServer(Java) - ...·  16 小时前    · 
踏实的洋葱  ·  如何在IStandaloneCodeEdit ...·  6 小时前    · 
火爆的香瓜  ·  qt graphics framework ...·  2 月前    · 
慷慨的丝瓜  ·  TabHost已经被弃用。·  8 月前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Encode the bytes-like object s, which can contain arbitrary binary data, and return bytes containing the base64-encoded data, with newlines (b'\n') inserted after every 76 bytes of output, and ensuring that there is a trailing newline, as per RFC 2045 (MIME).

Example:

>>> print(base64.encodebytes(b'a' * 100).decode())
YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFh
YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYQ==
  

Encode the string s, which can contain arbitrary binary data, and return a string containing one or more lines of base64-encoded data. encodestring() returns a string containing one or more lines of base64-encoded data always including an extra trailing newline ('\n').

The docs for version 2 could certainly be written more clearly, but it does what you want.

Example:

>>> print base64.encodestring('a'*100)
YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFh
YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYQ==

If you want line break, don't use the straight base64 package, use the one provided in email:

import email
print email.base64MIME.encode(your_string)

It do split the encoded string each 76 characters

This adds a newline. Try a longer string & you'll see. Also, "...'strict', meaning that encoding errors raise a UnicodeError)" docs.python.org/2/library/… – user_78361084 Sep 3, 2014 at 22:54

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.