Wraplines.py
Join the DZone community and get the full member experience.
Join For FreeApply a pattern to all lines in a text file.
Usage: wraplines.py source destination pattern [encoding]
Example: wraplines data.txt - '%s',\n\n iso-8859-13
from codecs import open
from sys import argv
def main():
if len(argv) < 4:
exit('usage: %s source destination pattern [encoding]' % argv[0])
sourcename, destname, pattern = argv[1:4]
pattern = pattern.decode('string_escape')
if destname == '-': destname = sourcename
try: charset = argv[4]
except IndexError: charset = 'utf-8'
source = open(sourcename, 'U', charset).read().split('\n')
dest = open(destname, 'w', charset)
for lines in xrange(len(source)):
dest.write(pattern % source.pop())
dest.close()
print '%d lines written to %s' % (lines * pattern.count('\n') + 1, destname)
if __name__ == '__main__':
main()
Opinions expressed by DZone contributors are their own.
Comments