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

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 ?

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)

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

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.