0

Hi


I want to print something like this to the standard o/p

I am currently doing
sys.stdout.writelines("%s %10s %10s\n" %(Folders,Folders,Folders))
but is not very well formatted how do I maintain column structure here

======================================================
Name Place Age
======================================================
John US 11
Mary UK 12
Mike US 14

How to do this using sys.stdout I am unable to give the exact spaces that can maintain colums

There is some sufficient space between all the columns whchi is not being visible in this post

3
Contributors
4
Replies
6
Views
8 Years
Discussion Span
Last Post by jlm699
0

What version of Python are you using here? On 2.5.2 I get the following:

>>> my_lines = [
...     "======================================================",
... "Name Place Age",
... "======================================================",
... "John US 11",
... "Mary UK 12",
... "Mike US 14" ]
>>> for line in my_lines:
...     if len(line.split()) == 3:
...         sys.stdout.writelines("%s %10s %10s\n" % tuple(line.split()))
...     else:
...         sys.stdout.writelines("%s\n" % line)
...     
======================================================
Name      Place        Age
======================================================
John         US         11
Mary         UK         12
Mike         US         14
>>>

Why are you using stdout.writelines ? Do you specifically need it or can you make use of print ?

0

Using sys.stdout.writelines() or sys.stdout.write() is kind of cumbersome and on Linux you also need a sys.stdout.flush() to make it work.

I think internally print(s), translates to sys.stdout.write(s)

0

What version of Python are you using here? On 2.5.2 I get the following:

>>> my_lines = [
...     "======================================================",
... "Name Place Age",
... "======================================================",
... "John US 11",
... "Mary UK 12",
... "Mike US 14" ]
>>> for line in my_lines:
...     if len(line.split()) == 3:
...         sys.stdout.writelines("%s %10s %10s\n" % tuple(line.split()))
...     else:
...         sys.stdout.writelines("%s\n" % line)
...     
======================================================
Name      Place        Age
======================================================
John         US         11
Mary         UK         12
Mike         US         14
>>>

Why are you using stdout.writelines ? Do you specifically need it or can you make use of print ?

Thanks for the reply,

I am using Python 2.6 , the strings that you are putting in tuple like "John US 11",
... "Mary UK 12",
... "Mike US 14"
are not simple strings that are declared, I get these as a result of some operations ,I actually loop through a dict and get these values , these values I want print in some order, I can't simply put them as strings
As shown above

Folder = q.to_dict();
 for i in range(len(Folder['table']['table"])):                     
#            print "Your Data is..."
#            print  "======================================================"  
#            print  "Name        Place       Age"
#            print  "======================================================"   
           
sys.stdout.writelines("%s %10s %10s\n" %(Folders['table']['table'][i]['name'],Folders['table']['table'][i]['data']['Place'],
Folders['table']['table'][i][Age']

I am trying to get name, Place, age from the above iteration

The reason I am using stdout.println is I thought it is more robust and print only supports simple printing

0

print only supports simple printing

What do you mean simple printing? I don't know of anything that stdout.write can do that print cannot...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.