473,388 Members | 1,256 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Formatting Output

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

The matter is that i don't know in advance how many columns there will
be. By the way, each column will be actually FLOATs, not INTs. How can
i do this ? Any help welcome. Regards,

Victor
Jun 27 '08 #1
8 3460
On Mon, 02 Jun 2008 00:34:09 -0700, vi************@gmail.com wrote:
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

The matter is that i don't know in advance how many columns there will
be. By the way, each column will be actually FLOATs, not INTs. How can
i do this ? Any help welcome. Regards,
Look at string methods, `join()` for example, and string formatting with
the ``%`` operator.

Ciao,
Marc 'BlackJack' Rintsch
Jun 27 '08 #2
On Jun 2, 9:34*am, "victor.hera...@gmail.com"
<victor.hera...@gmail.comwrote:
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

The matter is that i don't know in advance how many columns there will
be. By the way, each column will be actually FLOATs, not INTs. How can
i do this ? Any help welcome. Regards,

Victor
import sys
float_list = [1.0, 5.0, 6.0, 2.0, 2.0, 8.0, 2.0, 9.0, 5.0]
num_columns = 3
for i,item in enumerate(float_list):
sys.stdout.write('%.4f\t' % item)
if not (i+1) % num_columns:
sys.stdout.write('\n')

Problem with this approach is it doesn't cater for instances where you
exceed the standard 80 characters for a terminal window.
Jun 27 '08 #3
On Jun 2, 3:38*am, Chris <cwi...@gmail.comwrote:
On Jun 2, 9:34*am, "victor.hera...@gmail.com"

<victor.hera...@gmail.comwrote:
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
The matter is that i don't know in advance how many columns there will
be. By the way, each column will be actually FLOATs, not INTs. How can
i do this ? Any help welcome. Regards,
Victor

import sys
float_list = [1.0, 5.0, 6.0, 2.0, 2.0, 8.0, 2.0, 9.0, 5.0]
num_columns = 3
for i,item in enumerate(float_list):
* * sys.stdout.write('%.4f\t' % item)
* * if not (i+1) % num_columns:
* * * * sys.stdout.write('\n')

Problem with this approach is it doesn't cater for instances where you
exceed the standard 80 characters for a terminal window.
That wouldn't be a problem if being re-directed to a file.

A bigger problem would be if his list were actually

[1.0,2.0,2.0,5.0,2.0,9.0,6.0,8.0,5.0]

but he still wanted it printed

1 5 6
2 2 8
2 9 5

as if he always wants 3 rows, but doesn't know how many columns
it will take. In which case, he could do something like this:

import sys
float_list = [1.0,2.0,2.0,5.0,2.0,9.0,6.0,8.0,5.0]
num_rows = 3
num_cols = divmod(len(float_list),num_rows)
for r in xrange(num_rows):
for c in xrange(num_cols[0]):
sys.stdout.write('%.4f\t' % float_list[c*num_rows + r])
if num_cols[1]>0 and r<num_cols[1]:
sys.stdout.write('%.4f\t' % float_list[(c+1)*num_rows + r])
sys.stdout.write('\n')

1.0000 5.0000 6.0000
2.0000 2.0000 8.0000
2.0000 9.0000 5.0000

And then changing list size would merely add more columns, so

[1.0,2.0,2.0,5.0,2.0,9.0,6.0,8.0,5.0,6.6666]

would print as

1.0000 5.0000 6.0000 6.6666
2.0000 2.0000 8.0000
2.0000 9.0000 5.0000
Jun 27 '08 #4
On Mon, 2 Jun 2008 12:42:12 -0700 (PDT), Mensanator <me********@aol.comwrote:
On Jun 2, 3:38*am, Chris <cwi...@gmail.comwrote:
On Jun 2, 9:34*am, "victor.hera...@gmail.com"

<victor.hera...@gmail.comwrote:
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 =
------------------------------------------------------------------------------
~
Jun 27 '08 #5
On Jun 2, 9:43*pm, Doug Morse <mo...@edoug.orgwrote:
On Mon, 2 Jun 2008 12:42:12 -0700 (PDT), Mensanator <mensana...@aol.comwrote:
*On Jun 2, 3:38*am, Chris <cwi...@gmail.comwrote:
On Jun 2, 9:34*am, "victor.hera...@gmail.com"
<victor.hera...@gmail.comwrote:
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 notonly
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
Can't seem to do this with dictionaries but...

preformatted_string = """
%s %20s %20s
%s %30s
%s %30s
"""

print preformatted_string % ('first name'[:20], 'contact num 1'[:20],
'contact num type'[:20], 'address line 1'[:30], 'address line
2'[:30]
'address line 3'[:30], 'address line 4'[:30])

You could do something like that. the "[:20]" etc @ the end of the
inputs is ofc to trim the strings to a max length. The string
formatter supports "%<number of characters to move to the right>s" so
you can use that for alignment. It's a bit late so maybe I buggered
up when I tried to use dictionary assignment with it, but who knows :p
Jun 27 '08 #6
On Jun 2, 11:34*pm, Chris <cwi...@gmail.comwrote:
On Jun 2, 9:43*pm, Doug Morse <mo...@edoug.orgwrote:
On Mon, 2 Jun 2008 12:42:12 -0700 (PDT), Mensanator <mensana...@aol.com>wrote:
*On Jun 2, 3:38*am, Chris <cwi...@gmail.comwrote:
On Jun 2, 9:34*am, "victor.hera...@gmail.com"
<victor.hera...@gmail.comwrote:
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

Can't seem to do this with dictionaries but...

preformatted_string = """
%s %20s %20s
%s %30s
%s %30s
"""

print preformatted_string % ('first name'[:20], 'contact num 1'[:20],
* * * * 'contact num type'[:20], 'address line 1'[:30], 'address line
2'[:30]
* * * * 'address line 3'[:30], 'address line 4'[:30])

You could do something like that. *the "[:20]" etc @ the end of the
inputs is ofc to trim the strings to a max length. *The string
formatter supports "%<number of characters to move to the right>s" so
you can use that for alignment. *It's a bit late so maybe I buggered
up when I tried to use dictionary assignment with it, but who knows :p
Actually just realised I had the number on the wrong side... :D

preformatted_string = """
%(first_name)s %(contact_num)20s %(contact_type)20s
"""
print preformatted_string % {'first_name':'Chris',
'contact_num':'555-5555', 'contact_type':'Home'}
Jun 27 '08 #7
On Jun 2, 2:43*pm, Doug Morse <mo...@edoug.orgwrote:
On Mon, 2 Jun 2008 12:42:12 -0700 (PDT), Mensanator <mensana...@aol.comwrote:
*On Jun 2, 3:38*am, Chris <cwi...@gmail.comwrote:
On Jun 2, 9:34*am, "victor.hera...@gmail.com"
<victor.hera...@gmail.comwrote:
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 notonly
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
.
.
.
Jun 27 '08 #8
On 06:15, martedì 03 giugno 2008 Mensanator wrote:
In Access, I create a query with this SQL:
But this isn't python itself.
I'd like to see a small function to let 'locate' the cursor into a TTY
console. Surely it can't scroll.
If it is not possible, then ncurses is the way. I don't know if it works on
win32.

--
Mailsweeper Home : http://it.geocities.com/call_me_not_now/index.html
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert | last post by:
Hello, I am trying to get output formatted from a non-php script that I post to. Example: <form method=POST action=www.myurl.com/ColdFusion.cfm> bla bla bla </form>
5
by: lecichy | last post by:
Hello. As in the topic, I use www to execute shell command, in this case 'ls -l' and i get output like: total 8 drwx------ 11 lecichy staff 4096 Oct 15 18:18 Maildir drwx---r-x 3...
5
by: Tom Petersen | last post by:
I am using a response.write to test the formatting of the output. I am supposed to get this: BEGIN:VCALENDAR VERSION:1.0 BEGIN:VEVENT DTSTART:20051022T090000Z DTEND:20051022T090000Z...
6
by: Tom Petersen | last post by:
Here is a little more info, sorry should have explained what my final goal was. I am creating a .vcs file from a form to import into Outlook. I was just testing the output on screen then pasting...
1
by: Steve | last post by:
Hello, I need help with formatting an output of an array. I have a two dimensional array that looks something like this: x x x x y y y y b b b b ....and so on...I use to just...
1
by: D. Shane Fowlkes | last post by:
Hey guys. I have a Repeater and a Template. One of the dataitems calls a helper function. The dataitem sends the record ID to the function and the function runs a complex query and returns a...
3
by: mrdude | last post by:
i'm working on a program where one of the functions is to build mailing labels from a list of prospects. i'm having trouble figuring out how to set this up. i'm using vb.net and an access database....
4
Sheepman
by: Sheepman | last post by:
My quest, to put 100 random numbers in ten rows of ten. Then output the same to the screen and a file. My screen output is working,yeah! Text file, not so much. The data is getting there but not...
2
by: victor.herasme | last post by:
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 The matter is that i don't know in advance how many columns...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.