On Jun 23, 12:47 pm, Mensanator <[EMAIL PROTECTED]> wrote: > On Jun 23, 12:12 pm, [EMAIL PROTECTED] wrote: > > > > > > > Hi All, > > > How do I format printed data in python? > > I could not find this in the Python Reference > > Manual:http://docs.python.org/ref/print.html > > Nor could I find it in Matloff's great > > tutorial:http://heather.cs.ucdavis.edu/~matloff/Python/PythonIntro.pdf > > > For example, how do I turn this: > > > 512 Jun 5 2004 X11r6 > > 22 Jan 17 2005 a2p > > 22 Jan 17 2005 acctcom > > 5374 Sep 15 2002 acledit > > 5664 May 13 2004 aclget > > 12020 May 13 2004 aclput > > 115734 Jun 2 2004 adb > > 46518 Jun 4 2004 admin > > 66750 Sep 16 2002 ali > > 1453 Sep 15 2002 alias > > 28150 Jun 4 2004 alog > > 15 May 12 2005 alstat > > > into this: > > > 512 Jun 5 2004 X11r6 > > 22 Jan 17 2005 a2p > > 22 Jan 17 2005 acctcom > > 5374 Sep 15 2002 acledit > > 5664 May 13 2004 aclget > > 12020 May 13 2004 aclput > > 115734 Jun 2 2004 adb > > 46518 Jun 4 2004 admin > > 66750 Sep 16 2002 ali > > 1453 Sep 15 2002 alias > > 28150 Jun 4 2004 alog > > 15 May 12 2005 alstat > > > Thank you > > You could do this: > > data = ['512 Jun 5 2004 X11r6 ', \ > '22 Jan 17 2005 a2p', \ > '22 Jan 17 2005 acctcom ', \ > '5374 Sep 15 2002 acledit ', \ > '5664 May 13 2004 aclget ', \ > '12020 May 13 2004 aclput ', \ > '115734 Jun 2 2004 adb ', \ > '46518 Jun 4 2004 admin ', \ > '66750 Sep 16 2002 ali ', \ > '1453 Sep 15 2002 alias ', \ > '28150 Jun 4 2004 alog ', \ > '15 May 12 2005 alstat '] > > for i in data: > d = i.split() > print d[0].ljust(9), > print d[1].ljust(6), > print d[2].ljust(4), > print d[3].ljust(7), > print d[4].ljust(9) > > which gives you > > 512 Jun 5 2004 X11r6 > 22 Jan 17 2005 a2p > 22 Jan 17 2005 acctcom > 5374 Sep 15 2002 acledit > 5664 May 13 2004 aclget > 12020 May 13 2004 aclput > 115734 Jun 2 2004 adb > 46518 Jun 4 2004 admin > 66750 Sep 16 2002 ali > 1453 Sep 15 2002 alias > 28150 Jun 4 2004 alog > 15 May 12 2005 alstat > > or perhaps this: > > for i in data: > d = i.split() > print d[0].rjust(9), > print d[1].ljust(6), > print d[2].zfill(2).ljust(4), > print d[3].ljust(7), > print d[4].ljust(9) > > which gives this (if you want the digits in the numbers to > line up): > > 512 Jun 05 2004 X11r6 > 22 Jan 17 2005 a2p > 22 Jan 17 2005 acctcom > 5374 Sep 15 2002 acledit > 5664 May 13 2004 aclget > 12020 May 13 2004 aclput > 115734 Jun 02 2004 adb > 46518 Jun 04 2004 admin > 66750 Sep 16 2002 ali > 1453 Sep 15 2002 alias > 28150 Jun 04 2004 alog > 15 May 12 2005 alstat
In retrospect, that looks kind of clunky. If it was for my use, I'd do for i in data: d = i.split() print d[0].rjust(9), print d[1].rjust(6), print d[2].zfill(2), print d[3].ljust(7), print d[4].ljust(9) which looks like: 512 Jun 05 2004 X11r6 22 Jan 17 2005 a2p 22 Jan 17 2005 acctcom 5374 Sep 15 2002 acledit 5664 May 13 2004 aclget 12020 May 13 2004 aclput 115734 Jun 02 2004 adb 46518 Jun 04 2004 admin 66750 Sep 16 2002 ali 1453 Sep 15 2002 alias 28150 Jun 04 2004 alog 15 May 12 2005 alstat -- http://mail.python.org/mailman/listinfo/python-list