简体   繁体   中英

Python: printing a list as a table using texttable

I want to use texttable to print a list as a table.

This is my code:

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

As you can see above I have a break point which I have used to print some data for you to see, basically each row has 53 element in it. Somehow this basic code is giving me a very strange error which I can not decipher. The trace is provided below at the end. What am I doing wrong? What is the reason for 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, 0, 0, 0, 0, 0, 0, 0]
['TA | Entertainment', 0,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, 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)

I managed to solve this simply by trial and error. Since I had a big table (+50 or so columns) it could not print it without proper width information for each cell.

Once I provided an array with the width of each cell it worked right away.

table.set_cols_width(col_width)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM