2017-10-14 52 views
-1

我想用Python2.7中的漂亮表格模塊打印出一個表格格式的多維數組。使用python打印出多維數組美麗的表模塊

我曾嘗試下面的代碼:

from beautifultable import BeautifulTable 
tableA = BeautifulTable() 
tableA.column_headers = ["possible Inflow", "prob", "possibleinlow * prob", "d12p"] 
data = [['a', 1, 2, 2], [4.0, 3.0, 3.0, 2.0], [2.0, 4.0, 1.0, 4.0], [ 8., 12., 3., 8.], [ 1458., 3136., 784., 3364.]] 
tableA.column_headers = ["inputs", "possible Inflow", "prob", "possibleinlow * prob", "d12p"] 
for i in range(len(data)): 
    tableA.append_row([item[i] for item in data]) 
print(tableA) 

但它打印出以下錯誤:

Traceback (most recent call last):File "test.py", line 16, in <module> 

"raise ValueError("'Expected iterable of length {}, got {}".format(self._column_count, len(row))) 

ValueError異常:「長度爲4的預計迭代,得到5"

回答

1

@ Imm,假設你的第一個

tableA.column_headers = 

是你想要的(4項),你有4項5個陣列要添加到表中爲行則可能需要的程序simplier:

from beautifultable import BeautifulTable 
tableA = BeautifulTable() 
tableA.column_headers = ["possible Inflow", "prob", "possibleinlow * prob", "d12p"] 
data = [['a', 1, 2, 2], [4.0, 3.0, 3.0, 2.0], [2.0, 4.0, 1.0, 4.0],\ 
[8., 12., 3., 8.], [ 1458., 3136., 784., 3364.]] 
for i in range(len(data)): 
    tableA.append_row(data[i]) 
print(tableA) 

...產生:

+-----------------+------+----------------------------------------------+------+ 
| possible Inflow | prob |    possibleinlow * prob    | d12p | 
+-----------------+------+----------------------------------------------+------+ 
|  a  | 1 |      2      | 2 | 
+-----------------+------+----------------------------------------------+------+ 
|  4  | 3 |      3      | 2 | 
+-----------------+------+----------------------------------------------+------+ 
|  2  | 4 |      1      | 4 | 
+-----------------+------+----------------------------------------------+------+ 
|  8  | 12 |      3      | 8 | 
+-----------------+------+----------------------------------------------+------+ 
|  1458  | 3136 |      784      | 3364 | 
+-----------------+------+----------------------------------------------+------+ 

如果你不是想用第二

tabelA.column_headers = 

線和您的數據的列信息組,則只是簡單地使用的BeautifulTable的列方法之一。

from beautifultable import BeautifulTable 
tableA = BeautifulTable() 
columnHeaders = ["inputs", "possible Inflow", "prob", "possibleinlow * prob", "d12p"] 
data = [['a', 1, 2, 2], [4.0, 3.0, 3.0, 2.0], [2.0, 4.0, 1.0, 4.0],\ 
[8., 12., 3., 8.], [1458., 3136., 784., 3364.]] 
for i in range(len(columnHeaders)): 
    tableA.insert_column(i, columnHeaders[i], data[i]) 
print(tableA) 

...主要生產:

+--------+------------------------+------+------------------------------+------+ 
| inputs | possible Inflow  | prob |  possibleinlow * prob  | d12p | 
+--------+------------------------+------+------------------------------+------+ 
| a |   4   | 2 |    8    | 1458 | 
+--------+------------------------+------+------------------------------+------+ 
| 1 |   3   | 4 |    12    | 3136 | 
+--------+------------------------+------+------------------------------+------+ 
| 2 |   3   | 1 |    3    | 784 | 
+--------+------------------------+------+------------------------------+------+ 
| 2 |   2   | 4 |    8    | 3364 | 
+--------+------------------------+------+------------------------------+------+ 

添加一些打印線可以幫助理解的事情。 乾杯!

+0

舒馬克我實際上想要垂直排列它們 – Imm