reformat text file to make its lines shorter than a limitation
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import codecs
LINE_LIMIT = 61
P_START = u'\u3000\u3000'
def spawnline(u, us=None):
if us is None:
us = []
if len(u) > LINE_LIMIT:
nu = u[:LINE_LIMIT] + u'\n'
us.append(nu)
return spawnline(u[LINE_LIMIT:], us)
else:
us.append(u)
return us
def main():
input_name = sys.argv[1]
if (len(sys.argv) >= 3):
output_name = sys.argv[2]
else:
output_name = '_' + input_name
with codecs.open(input_name, 'r', 'utf-8') as input_file:
lines = []
for line in input_file.readlines():
if line.startswith(P_START):
line = line.lstrip(P_START)
lines += spawnline(line)
with codecs.open(output_name, 'w', 'utf-8') as output_file:
output_file.write(''.join(lines))
if __name__== '__main__':
main()