On Jun 2, 2:43 pm, Doug Morse <[EMAIL PROTECTED]> wrote: > On Mon, 2 Jun 2008 12:42:12 -0700 (PDT), Mensanator <[EMAIL PROTECTED]> wrote: > > On Jun 2, 3:38 am, Chris <[EMAIL PROTECTED]> wrote: > > > On Jun 2, 9:34 am, "[EMAIL PROTECTED]" > > > > <[EMAIL PROTECTED]> wrote: > > > > Hi, > > > > > i am building a little script and i want to output a series of columns > > > > more or less like this: > > > > > 1 5 6 > > > > 2 2 8 > > > > 2 9 5 > > ... > > I have a related question: > > Does Python have (or can emulate) the formatted output capability found in > Perl? > > For example, all I have to do to get nicely formatted (i.e., aligned) output > is provide values for special STDOUT variables (i.e., STDOUT_TOP, STDOUT, > STDOUT_BOTTOM, etc.), exemplified by: > > format STDOUT_TOP = > > ------------------------------------------------------------------------------ > ~ > . > > format STDOUT = > @<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<< > $res->{'full_name'}, $res->{'phone_1'}, $res->{'phone_1_type'} > @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~ > $res->{'address_1a'}, $res->{'address_2a'} > @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~ > $res->{'address_1b'}, $res->{'address_2b'} > @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~ > $res->{'address_1c'}, $res->{'address_2c'} > @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~ > $city_1 $city_2 > @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~ > $res->{'email_1'}, $res->{'email_2'} > > ------------------------------------------------------------------------------ > ~ > . > > Then, all I have to do is populate my $res object/hash as desired -- in this > example simple the results of a SQL query -- and lastly just call the "write" > function: > > write; > > and Perl will produce very nicely formatted results. This is useful not only > for producing human readable output, but also fixed-column-width data files, > etc. I'd love to learn the Pythonistic way of doing the same thing. > > Thanks! > Doug
I didn't know you could do that in perl. Too bad I quit using it when I switched to Python. OTOH, I can do similar formatting directly in SQL. In Access, I create a query with this SQL: SELECT "------------------------------------------------------------" & Chr(10) & Format$(SampleEventCode,"!@@@@@@@@@@@@@@@@") & Format$ (SampleEventDescr,"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@") & Chr(10) & Format$(EventSortOrder,"!@@@@@@@@@@@@@@@@") & Format$("From: " & Format$(DateFrom,"YYYY/MM/ DD"),"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@") & Chr(10) & Format$(SampleEventReportLabel,"!@@@@@@@@@@@@@@@@") & Format$("To: " & Format$(DateTo,"YYYY/MM/ DD"),"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@") AS Expr1 FROM tblSampleEvent; Then I simply call it from Python. (I could call with the SQL code instead of invoking the Access query, but the OBDC interface can't process this particular SQL statement, tries to put brackets around the format templates, so best render unto Access the things that belong to Access.) # code import dbi import odbc con = odbc.odbc("BPWR") cursor = con.cursor() cursor.execute(""" SELECT * FROM SQLFormatTest; """) results = cursor.fetchall() for i in results: for j in i: print j # /code which prints ------------------------------------------------------------ 2000Q1 First Quarter of 2000 2000100 From: 2000/01/01 1st-Qtr 2000 To: 2000/03/31 ------------------------------------------------------------ 2000Q2 Second Quarter of 2000 2000200 From: 2000/04/01 2nd-Qtr 2000 To: 2000/06/30 ------------------------------------------------------------ 2000Q3 Third Quarter of 2000 2000300 From: 2000/07/01 3rd-Qtr 2000 To: 2000/09/30 ------------------------------------------------------------ 2000Q4 Fourth Quarter of 2000 2000400 From: 2000/10/01 4th-Qtr 2000 To: 2000/12/31 ------------------------------------------------------------ 2000Q1LF Low Flow First Quarter of 2000 2000105 From: 2000/01/01 1st-Qtr 2000 To: 2000/03/31 ------------------------------------------------------------ 2000Q2LOD LOD borings Second Quarter of 2000 2000206 From: 2000/04/01 2nd-Qtr 2000 To: 2000/06/30 . . . -- http://mail.python.org/mailman/listinfo/python-list