-2

I am quite struggling with as I tried many libraries to print table but no success - so I thought to post here and ask.

My data is in a text file (resource.txt) which looks like this (the exact same way it prints)

pipelined  8   8  0  17  0   0
nonpipelined  2 2  0  10  0  0 

I want my data print in the following manner

Design name       LUT       Lut as m    Lut as I    FF  DSP   BRAM
-------------------------------------------------------------------    
pipelined          8          8            0        17  0      0
Non piplined       2          2            0        10  0      0

Some time data may be more line column remain same but rows may increase.

(i have python 2.7 version)

I am using this part in my python code all code working but am couldn't able print data which i extracted to text file in tabular form. As I can't use panda library as it won't support for python 2.7, but I can use tabulate and all library. Can anyone please help me?

I tried using tabulate and all but I keep getting errors.

I tried at end simple method to print but its not working (same code works if I put at top of code but at the end of code this won't work). Does anyone have any idea?

q11=open( "resource.txt","r")
for line in q11:
   print(line)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Aadhya Sharma
  • 25
  • 1
  • 1
  • 6

3 Answers3

3

Here's a self contained function that makes a left-justified, technical paper styled table.

def makeTable(headerRow,columnizedData,columnSpacing=2):
    """Creates a technical paper style, left justified table

    Author: Christopher Collett
    Date: 6/1/2019"""
    from numpy import array,max,vectorize

    cols = array(columnizedData,dtype=str)
    colSizes = [max(vectorize(len)(col)) for col in cols]

    header = ''
    rows = ['' for i in cols[0]]

    for i in range(0,len(headerRow)):
        if len(headerRow[i]) > colSizes[i]: colSizes[i]=len(headerRow[i])
        headerRow[i]+=' '*(colSizes[i]-len(headerRow[i]))
        header+=headerRow[i]
        if not i == len(headerRow)-1: header+=' '*columnSpacing

        for j in range(0,len(cols[i])):
            if len(cols[i][j]) < colSizes[i]:
                cols[i][j]+=' '*(colSizes[i]-len(cols[i][j])+columnSpacing)
            rows[j]+=cols[i][j]
            if not i == len(headerRow)-1: rows[j]+=' '*columnSpacing

    line = '-'*len(header)
    print(line)
    print(header)
    print(line)
    for row in rows: print(row)
    print(line)

And here's an example using this function.

>>> header = ['Name','Age']
>>> names = ['George','Alberta','Frank']
>>> ages = [8,9,11]
>>> makeTable(header,[names,ages])
------------
Name     Age
------------
George   8    
Alberta  9    
Frank    11   
------------
Chris Collett
  • 1,074
  • 10
  • 15
0

Since the number of columns remains the same, you could just print out the first line with ample spaces as required. Ex-

    print("Design name",'       ',"LUT",'       ',"Lut as m",'    ',"and continue 
    like that")

Then read the csv file. datafile will be

    datafile = open('resource.csv','r')
    reader = csv.reader(datafile)
    for col in reader:
        print(col[0],'          ',col[1],'       ',col[2],'      ',"and 
        continue depending on the number of columns")

This is not he optimized solution but since it looks like you are new, therefore this will help you understand better. Or else you can use row_format print options in python 2.7.

Anurag Reddy
  • 1,159
  • 11
  • 19
0

Here is code to print table in nice table, you trasfer all your data to sets then you can data or else you can trasfer data in text file line to one set and print it

from beautifultable import BeautifulTable


h0=["jkgjkg"]
h1=[2,3]
h2=[2,3]
h3=[2,3]
h4=[2,3]
h5=[2,3]

h0.append("FPGA resources")

table = BeautifulTable()
table.column_headers = h0
table.append_row(h1)
table.append_row(h2)
table.append_row(h3)
table.append_row(h4)
table.append_row(h5)
print(table)

Out Put:

+--------+----------------+
| jkgjkg | FPGA resources |
+--------+----------------+
|   2    |       3        |
+--------+----------------+
|   2    |       3        |
+--------+----------------+
|   2    |       3        |
+--------+----------------+
|   2    |       3        |
+--------+----------------+
|   2    |       3        |
+--------+----------------+
Aadhya Sharma
  • 25
  • 1
  • 1
  • 6