如何使用Python生成表

时间:2018-08-07 15:15:26

标签: python python-2.7

当我尝试使用许多库来打印表格但没有成功时,我感到非常困惑

我的数据在文本文件(resource.txt)

看起来像这样(与打印完全相同)

pipelined  8   8  0  17  0   0

nonpipelined  2 2  0  10  0  0 

我希望以以下方式打印数据

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

有时数据可能会更多,但Colom行保持不变,但行数可能会增加。

(我有python 2.7版本)

我在我的python代码中使用了这一部分,所有代码都能正常工作,但无法打印以表格形式提取到文本文件的数据。由于我不能使用panda liberary,因为它不支持python 2.7,但是我可以使用制表法和所有的lib.ary。有人可以帮助我。我尝试使用制表法以及除给出错误以外的所有方法

我尝试了一种简单的打印方法,但它无法正常工作(如果我将代码放在代码的顶部,则相同的代码可以工作,但是在代码的末尾,这将无法工作..任何人都知道

q11=open( "resource.txt","r")
for line in q11:
   print(line)

3 个答案:

答案 0 :(得分:1)

这里是在漂亮的表中打印表的代码,您将所有数据传输到集合中,然后可以进行数据传输,否则可以将文本文件行中的数据传输到一组并打印

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)

输出:

+--------+----------------+
| jkgjkg | FPGA resources |
+--------+----------------+
|   2    |       3        |
+--------+----------------+
|   2    |       3        |
+--------+----------------+
|   2    |       3        |
+--------+----------------+
|   2    |       3        |
+--------+----------------+
|   2    |       3        |
+--------+----------------+

答案 1 :(得分:1)

这是一个自包含函数,可创建左对齐的技术论文样式表。

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)

这是使用此功能的示例。

>>> header = ['Name','Age']
>>> names = ['George','Alberta','Frank']
>>> ages = [8,9,11]
>>> makeTable(header,[names,ages])
------------
Name     Age
------------
George   8    
Alberta  9    
Frank    11   
------------

答案 2 :(得分:0)

由于列数保持不变,因此您可以根据需要打印出第一行,并留出足够的空格。

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

然后读取csv文件。数据文件将是

    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")

这不是他的优化解决方案,但由于它看起来像您一样,因此可以帮助您更好地理解。否则,您可以在python 2.7中使用row_format打印选项。