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 for loop which should print all the columns and rows which data_from_variable carrying.
thanks

Recommended Answers

All 3 Replies

You want to print as a matrix, or such?

# A simple matrix
# This matrix is a list of lists
# Column and row numbers start with 1
 
class Matrix(object):
    def __init__(self, cols, rows):
        self.cols = cols
        self.rows = rows
        # ;;initialize matrix and fill with zeroes
        self.matrix = []
        for i in range(rows):
            ea_row = []
            for j in range(cols):
                ea_row.append(0)
            self.matrix.append(ea_row)
 
    def setitem(self, col, row, v):
        self.matrix[col-1][row-1] = v
 
    def getitem(self, col, row):
        return self.matrix[col-1][row-1]
 
    def __repr__(self):
        outStr = ""
        for i in range(self.rows):
            outStr += 'Row %s ;;= %s\n' % (i+1, self.matrix[i])
        return outStr
 
 
a = Matrix(4,4)
print a
a.setitem(3,4,'55.75')
print a
a.setitem(2,3,'19.1')
print a
print a.getitem(3,4)

I got this code off bvdet post on http://bytes.com/topic/python/answers/594203-please-how-create-matrix-python
I hope he won't mind it, as long as his code helps others.:D

You want to print as a matrix, or such?

# A simple matrix
# This matrix is a list of lists
# Column and row numbers start with 1
 
class Matrix(object):
    def __init__(self, cols, rows):
        self.cols = cols
        self.rows = rows
        # ;;initialize matrix and fill with zeroes
        self.matrix = []
        for i in range(rows):
            ea_row = []
            for j in range(cols):
                ea_row.append(0)
            self.matrix.append(ea_row)
 
    def setitem(self, col, row, v):
        self.matrix[col-1][row-1] = v
 
    def getitem(self, col, row):
        return self.matrix[col-1][row-1]
 
    def __repr__(self):
        outStr = ""
        for i in range(self.rows):
            outStr += 'Row %s ;;= %s\n' % (i+1, self.matrix[i])
        return outStr
 
 
a = Matrix(4,4)
print a
a.setitem(3,4,'55.75')
print a
a.setitem(2,3,'19.1')
print a
print a.getitem(3,4)

I got this code off bvdet post on http://bytes.com/topic/python/answers/594203-please-how-create-matrix-python
I hope he won't mind it, as long as his code helps others.:D

No, i dont want to print as matrix i simply want to read it from database ,here is my code which is only writing one row on output file , it is reading first row from all columns and printing them on file. but person_data variable has more column which i mentioned in code and rows . how can i print all the columns and rows which this variable has

result_set = connection.execute( "select * from user(" + userid + "," + username + "," + email + ")" )
		for row in result:
			line = row ['name'] + "\t" + str(row[ 'email' ]) + "\t" + str(row[ 'house_no' ]) + "\t" + str(row[ 'streat_name' ]) + "\r"
			out.write(line)
	
		out.close()
		connection.close()

You want to print as a matrix, or such?

# A simple matrix
# This matrix is a list of lists
# Column and row numbers start with 1
 
class Matrix(object):
    def __init__(self, cols, rows):
        self.cols = cols
        self.rows = rows
        # ;;initialize matrix and fill with zeroes
        self.matrix = []
        for i in range(rows):
            ea_row = []
            for j in range(cols):
                ea_row.append(0)
            self.matrix.append(ea_row)
 
    def setitem(self, col, row, v):
        self.matrix[col-1][row-1] = v
 
    def getitem(self, col, row):
        return self.matrix[col-1][row-1]
 
    def __repr__(self):
        outStr = ""
        for i in range(self.rows):
            outStr += 'Row %s ;;= %s\n' % (i+1, self.matrix[i])
        return outStr
 
 
a = Matrix(4,4)
print a
a.setitem(3,4,'55.75')
print a
a.setitem(2,3,'19.1')
print a
print a.getitem(3,4)

I got this code off bvdet post on http://bytes.com/topic/python/answers/594203-please-how-create-matrix-python
I hope he won't mind it, as long as his code helps others.:D

No, i dont want to print as matrix i simply want to read it from database ,here is my code which is only writing one row on output file , it is reading first row from all columns and printing them on file. but person_data variable has more column which i mentioned in code and rows . how can i print all the columns and rows which this variable has

result_set = connection.execute( "select * from user(" + userid + "," + username + "," + email + ")" )
		for row in result:
			line = row ['name'] + "\t" + str(row[ 'email' ]) + "\t" + str(row[ 'house_no' ]) + "\t" + str(row[ 'streat_name' ]) + "\r"
			out.write(line)
	
		out.close()
		connection.close()
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.