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 8 3454
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
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.
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
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 =
------------------------------------------------------------------------------
~
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
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'}
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
.
.
.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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>
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
| |