tags:

views:

148

answers:

2

I'm trying to find a pythonic way to do this PHP code:

chunk_split(base64_encode($picture));

http://us2.php.net/chunk_split

chunk_split split the string into smaller chunks of 76 character long by adding a "\r\n" (RFC 2045).

thank you

A: 
chunk_split = lambda s: '\r\n'.join(s[i:min(i+76, len(s))] for i in xrange(0, len(s), 76))
Ivan Baldin
+3  A: 

This should do it:

str.encode("base64").replace("\n", "\r\n")
Gumbo
This is not what I want to do. I don't want to replace "\n" with "\r\n"
`encode("base64")` is just using `\n` instead of `\r\n`.
Gumbo
Heh—why does this code, which doesn't address the question, have a score of 4, while the correct answer has a score of -1?
Miles
@Miles: that's because Gumbo answered the question being asked while I did it only "technically".@Gumbo: `base64.encodestring(s).replace('\n', '\r\n')` would be python 2.x and 3.x compatible while doing the same thing.
Ivan Baldin