-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)
1
  • Use pandas.read_csv Commented Aug 7, 2018 at 15:19

3 Answers 3

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   
------------
Sign up to request clarification or add additional context in comments.

Comments

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.

6 Comments

this is not working....reader is not recognizing its giving error dude..
did you import csv
Yes i did still nothing is printing
So you might have to convert the text file to a csv file. Please follow this link of converting text file to a csv file. stackoverflow.com/a/10220428/9530965
tell me other way
|
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        |
+--------+----------------+

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.