2017-10-14 4 views
-1

Python2.7のBeautiful Tableモジュールを使用して多次元配列を表形式で出力しようとしています。Pythonを使用して多次元配列を出力するBeautiful Tableモジュール

私は次のコードを試してみました:

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

を得ました@イム、あなたの最初のことを前提とします。

tableA.column_headers = 

は、あなたが望む1(4つの項目)で、行は、あなたがsimplierプログラムたいこととして、あなたがテーブルに追加されている4つの項目の5つのアレイを持っている:

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

shumack私は実際には垂直に並べ替えたい – Imm