How to give a single Tab Space between two or columns in Output In python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aliraza376
    New Member
    • Jan 2012
    • 1

    #1

    How to give a single Tab Space between two or columns in Output In python

    Code:
    import psycopg2
    import os.path
    
    conn = psycopg2.connect("dbname=overlapanalysis user=postgres")
    cur = conn.cursor()
    cur.execute("select * from  annotation(5,-10000,-10000) ")
    
    result=cur.fetchall()
    for columns in result:
        
        print (str(columns[1])+"\t"+str(columns[2])+"\t"+str(columns[3])+"\t"+str(columns[4])+"\t"+str(columns[5]));
    conn.commit()
    cur.close()
    conn.close()


    In this the picture is also upload of Out put the white spaces is to much i want only 1 Tab Space in every column.
    Attached Files
    Last edited by bvdet; Jan 23 '12, 10:20 PM. Reason: Add code tags. Please use code tags when posting code. [code]....[/code]
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    I would test the length of each column with code along the lines of this untested (since we don't have the DB) snippet
    Code:
    result=cur.fetchall()
    for columns in result:
        stop = 6
        if len(columns) < 6:
            stop = len(columns)
        for num in range(1, stop):
            print(num, len(columns[num]))
        this_line = [str(columns[num]).strip() for num in range(1, stop)]
        print "\t".join(this_line)
    Also, the commit statement does nothing as you have not changed the DB.

    Comment

    Working...