This is a simple script to covert a top to bottom list of items into a left to right list.
a
b
c
d
e
f
g
h
i
j
k
l
m
into
a b c d e f g
h i j k l m
A few command line options allow some amount of customisation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import sys
import optparse
DEF_FSEPARATOR = " "
DEF_LWIDTH = 80
DEF_HEADER = ""
DEF_FOOTER = ""
DEF_LSEPARATOR = "\n"
def main(lwidth,fseparator,lseparator,header,footer):
try:
line = []
llen = 0
lensep = len(fseparator)
print header
for i in sys.stdin:
i = i.strip()
llen += len(i) + lensep
if llen <= lwidth:
line.append(i)
else:
print fseparator.join(line) + fseparator + lseparator,
line = []
llen = 0
except KeyboardInterrupt:
None
print footer
def parse_options():
parser = optparse.OptionParser(usage="%prog [options] [file]", version="%prog 0.1")
parser.add_option("-s","--fseparator",action="store",type="string",dest="fseparator",default = DEF_FSEPARATOR,
help="String used to separate fields. Defaults to '%s'"%DEF_FSEPARATOR)
parser.add_option("-w","--width",action="store",type="int",dest="width", default = DEF_LWIDTH,
help="Maximum width of the table in characters. Defaults to '%d'"%DEF_LWIDTH)
parser.add_option("-H","--header",action="store",type="string",dest="header", default = DEF_HEADER,
help="Header to be printed before tabulated output. Defaults to '%s'"%DEF_HEADER)
parser.add_option("-F","--footer",action="store",type="string",dest="footer", default = DEF_FOOTER,
help="Footer to be printed after tabulated output. Defaults to '%s'"%DEF_FOOTER)
parser.add_option("-l","--lseparator",action="store",type="string",dest="lseparator", default = DEF_LSEPARATOR,
help=r"String used to separate lines. Defaults to '%s'"%DEF_LSEPARATOR)
foo = parser.parse_args()
return foo
if __name__ == "__main__":
options,args = parse_options()
main(options.width,
options.fseparator,
options.lseparator,
options.header,
options.footer
)
|
This is useful when you want to print out a long list for reference (e.g a list of primes).
Tags: parsing, text_processing