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 3422
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: Rina0 |
last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
| |