Created Table From List Not Showing All Elements
I have a function that accept list as a parameter that was already created from another function. The list is printed in column row format. This function should assume that the fir
Solution 1:
All you need to do is loop over the rows in returned_list
, instead of hard-coding returned_list[0]
:
defshowList(returned_List):
header = ' '
line = ''
entries = ''for row in returned_list:
for i in row:
header += i + (' ')*5for k inrange(len(header)):
line += '='for j in returned_List[1]:
entries += j +(' ')*5print(header, '\n', line, '\n', entries)
return(returned_List)
From your comments, I kinda see what you are looking for. Here's an adaptation of script I wrote a while back that'll help you:
deftabularize(inData, outfilepath):
""" Return nothing
Write into the file in outfilepath, the contents of inData, expressed in tabular form.
The tabular form is similar to the way in which SQL tables are displayed.
"""
widths = [max([len(row) for row in rows])+2for rows in izip_longest(*inData, fillvalue="")]
withopen(outfilepath, 'w') as outfile:
outfile.write("+")
for width in widths:
outfile.write('-'*width + "+")
outfile.write('\n')
for line in lines:
outfile.write("|")
for col,width in izip_longest(line,widths, fillvalue=""):
outfile.write("%s%s%s|" %(' '*((width-len(col))/2), col, ' '*((width+1-len(col))/2)))
outfile.write('\n+')
for width in widths:
outfile.write('-'*width + "+")
outfile.write('\n')
if __name__ == "__main__":
print'starting'
tabularize(infilepath, outfilepath, '...', False)
print'done'
Hope this helps
Solution 2:
for i in returned_list[0]: only looks at the first entry.
you probably want:
for i in returned_list:
Post a Comment for "Created Table From List Not Showing All Elements"