7 Replies - 2083 Views - Last Post: 22 February 2012 - 12:35 PM Rate Topic: -----

#1 pythonuser  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 15
  • Joined: 21-February 12

How to print data as gridview

Posted 21 February 2012 - 12:42 AM

hi ,

how can i print data as grid view in python, in my case a variable is carrying all the data which is coming from table and i want to print them by using two nested loops.

 data_from_table = ['col 1'] + "/t" + ['col 2'] + "/t" + ----------------- ['col n'] 



here data_from_table is my variable name which has all the columns and rows i need the code which should print all the columns and rows which data_from_variable carrying.
thanks

Is This A Good Question/Topic? 0
  • +

Replies To: How to print data as gridview

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 797
  • Joined: 08-June 10

Re: How to print data as gridview

Posted 21 February 2012 - 11:04 AM

Using two for loops you can iterate through rows then columns of an array. Assuming Python 2.x, you can use a comma at the end of a print statement to prevent the a newline; you then issue a final print statement at the end of each row to force a newline to be printed:
for row in data_from_table:
    for column in row:
        print column,
    print ""



In Python 3, you will need to use the end keyword argument in order to suppress the newline:
for row in data_from_table:
    for column in row:
        print(column, end="")
    print("")


Was This Post Helpful? 0
  • +
  • -

#3 pythonuser  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 15
  • Joined: 21-February 12

Re: How to print data as gridview

Posted 22 February 2012 - 01:54 AM

View PostMotoma, on 21 February 2012 - 11:04 AM, said:

Using two for loops you can iterate through rows then columns of an array. Assuming Python 2.x, you can use a comma at the end of a print statement to prevent the a newline; you then issue a final print statement at the end of each row to force a newline to be printed:
for row in data_from_table:
    for column in row:
        print column,
    print ""



In Python 3, you will need to use the end keyword argument in order to suppress the newline:
for row in data_from_table:
    for column in row:
        print(column, end="")
    print("")



Thanks for help ,but i have few things to ask , i want to write all the column and rows on a file and those column must be tab separated here is sample code which prints only one row from only those column which i have mentioned but this data_from_table variable has some other columns , as you said your code will fetch all the columns which that data_from_table variable has and it will also print all the rows,i want to know how can i write columns and rows on file. thanks

if person == 'person_1':
	
		engine = create_engine(con_str)
		connection = engine.connect()
		data_from_table = connection.execute( "select * from person_1(" + personid + "," + personname + "," + personsirname + ")" )
		for row in data_from_table:
                #your code fits here but i want to print them on file as out.write(line) is doing and those columns mut be tab saparated.
			line = row ['name'] + "\t" + str(row[ 'age' ]) + "\t" + str(row[ 'contactinfo' ]) + "\t" + str(row[ 'email' ]) + "\t" + str(row[ 'homeaddress' ]) + "\r"
			out.write(line)
	
		out.close()
		connection.close()


This post has been edited by Motoma: 22 February 2012 - 06:48 AM
Reason for edit:: Added [code] tags.

Was This Post Helpful? 0
  • +
  • -

#4 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 797
  • Joined: 08-June 10

Re: How to print data as gridview

Posted 22 February 2012 - 06:53 AM

Looking in your code, I don't see any reason why this would cause problems. I don't know the specifics of your engine but the most likely reasons that you are only saving one row to a file are:
  • Your query is only returning one row (most likely).
  • The data_from_table object is not properly iterating in your for loop on line 6.

Was This Post Helpful? 0
  • +
  • -

#5 pythonuser  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 15
  • Joined: 21-February 12

Re: How to print data as gridview

Posted 22 February 2012 - 08:25 AM

View PostMotoma, on 22 February 2012 - 06:53 AM, said:

Looking in your code, I don't see any reason why this would cause problems. I don't know the specifics of your engine but the most likely reasons that you are only saving one row to a file are:
  • Your query is only returning one row (most likely).
  • The data_from_table object is not properly iterating in your for loop on line 6.


Quote

Yes , your right my query is return one row , how can i iterate all columns and rows in for loop, and forget about the connect_engine its the requirement of the project to which i am working,alli want to know how can i iterate for loop for columns and rows.
thanks

Was This Post Helpful? 0
  • +
  • -

#6 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 797
  • Joined: 08-June 10

Re: How to print data as gridview

Posted 22 February 2012 - 08:31 AM

I see now. It entirely depends on the object type of data_from_table, if it is a common list of dictionary, you may just be able to iterate over keys:

for row in data_from_table:
    for key in row.keys():
        out.write(row[key] + "\t")
    out.write("\n")



The technique here is just to tailor my original loop to fit your data types.
Was This Post Helpful? 0
  • +
  • -

#7 pythonuser  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 15
  • Joined: 21-February 12

Re: How to print data as gridview

Posted 22 February 2012 - 09:49 AM

View PostMotoma, on 22 February 2012 - 08:31 AM, said:

I see now. It entirely depends on the object type of data_from_table, if it is a common list of dictionary, you may just be able to iterate over keys:

for row in data_from_table:
    for key in row.keys():
        out.write(row[key] + "\t")
    out.write("\n")



The technique here is just to tailor my original loop to fit your data types.


once again thanks for help , i tried your code but it is giving me error
which is out.write(row[key] + "\t")
type error: unsupported operand types for +: 'int' and 'str'
now how can we iterate with this datatype . i really appreciate your help
thanks
Was This Post Helpful? 0
  • +
  • -

#8 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 797
  • Joined: 08-June 10

Re: How to print data as gridview

Posted 22 February 2012 - 12:35 PM

Well, if you've got an int, you'll need to cast it to a string before you can concatenate. str(row[key]) should work fine.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1