Python:使用texttable将列表打印为表

时间:2014-07-01 09:52:06

标签: python list

我想使用texttable将列表打印为表格。

这是我的代码:

col_types = ['t']
col_names = ['sessions/week']
col_align = ['r']
weeks_result = []
for i in range(1,53):
    col_types.append("t")
    col_names.append(str(i))
    col_align.append("r")
table = Texttable()
table.set_deco(Texttable.HEADER)
table.set_cols_dtype(col_types)
table.set_cols_align(col_align)
table.add_row(col_names)

import pdb; pdb.set_trace()
for area, results_per_week in areas_weeks_dic.items():
    weeks_result.append(area)
    for i in range(1,53):
        weeks_result.append(results_per_week[str(i)])
    print weeks_result
    table.add_row(weeks_result)
    weeks_result = []

print table.draw()

正如你在上面所看到的,我有一个断点,我用它来打印一些数据供你查看,基本上每一行都有53个元素。 不知何故,这个基本代码给了我一个非常奇怪的错误,我无法破译。 跟踪在下面提供。我究竟做错了什么? ValueError的原因是什么?

> c:...estprocess.py(600)<module>()
-> for area, results_per_week in areas_weeks_dic.items():
(Pdb) print col_types
['t', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't',
 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't']
(Pdb) print col_names
['sessions/week', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24
', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49
', '50', '51', '52']
(Pdb) print col_align
['r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r',
 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r']
(Pdb) c
['TA | BaseConcept', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 5, 10, 12, 16, 29, 17, 24, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
['TA | Telephony', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 5, 10, 12, 16, 29, 17, 24, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
['TA | Driver Information', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 5, 10, 12, 16, 29, 17, 24, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
['TA | Telematics', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 5, 10, 12, 16, 29, 17, 24, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
['TA | Vehicle Functions', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 5, 10, 12, 16, 29, 17, 24, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
['TA | Audio', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 5, 10, 12, 16, 29, 17, 24, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0,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0]
['TA | Navigation', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 5, 10, 12, 16, 29, 17, 24, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
['TA | Connectivity', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 5, 10, 12, 16, 29, 17, 24, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
['TA | Smoke Test', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 5, 10, 12, 16, 29, 17, 24, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Traceback (most recent call last):
  File "GenerateTestProcess.py", line 600, in <module>
    for area, results_per_week in areas_weeks_dic.items():
  File "GenerateTestProcess.py", line 324, in draw
    out += self._draw_line(row)
  File "GenerateTestProcess.py", line 493, in _draw_line
    line = self._splitit(line, isheader)
  File "GenerateTestProcess.py", line 534, in _splitit
    array.extend(textwrap.wrap(c, width))
  File "C:\Python27\lib\textwrap.py", line 354, in wrap
    return w.wrap(text)
  File "C:\Python27\lib\textwrap.py", line 329, in wrap
    return self._wrap_chunks(chunks)
  File "C:\Python27\lib\textwrap.py", line 258, in _wrap_chunks
    raise ValueError("invalid width %r (must be > 0)" % self.width)
ValueError: invalid width -2 (must be > 0)

1 个答案:

答案 0 :(得分:0)

我设法通过反复试验来解决这个问题。 由于我有一个大表(+50 or so columns),如果没有适合每个单元格的宽度信息,它就无法打印出来。

一旦我提供了一个数组,其中每个单元格的宽度都可以立即生效。

table.set_cols_width(col_width)