Using Python I need to insert a newline character into a string every 64 characters. In Perl it\'s easy:
s/(.{64})/$1\\n/
How could this be
itertools has a nice recipe for a function grouper that is good for this, particularly if your final slice is less than 64 chars and you don't want a slice error:
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
Use like this:
big_string =
output = '\n'.join(''.join(chunk) for chunk in grouper(big_string, 64))