0

I want to convert data into tabulate form. After installing and importing tabulate module I give following command. Where training_accuracy, test_accuracy and training_step are python list. I am getting the error of invalid syntax. I came across the following command that I want to use

`print tabulate([['Alice', 24], ['Bob', 19]], headers=['Name', 'Age'])`

But my training lists are quite long I can not type it again and again. How can I fix this error

`print tabulate([[training_step],[train_accuracy], [test_accuracy]], 
headers=['Step', 'Train_accuracy', 'Test_accuracy'])`

Here is my error SyntaxError: invalid syntax

herry
  • 155
  • 10
  • I tried without passing strings but still getting error of invalid formate – herry Oct 14 '18 at 01:51
  • Then post the code that fails as an [mcve](https://stackoverflow.com/help/mcve) along with the full error. – Craig Oct 14 '18 at 01:53
  • Are you using python 2 or 3? – Craig Oct 14 '18 at 01:57
  • I am using python3 – herry Oct 14 '18 at 01:58
  • `print` is a function call, so you need to wrap the arguments in `()`. As in `print( tabulate( ... ) )`. – Craig Oct 14 '18 at 02:00
  • I hardly installed tabular module Now I want to use that module. Can you please guide me How to convert data into tabular form by this. Or with some for loop – herry Oct 14 '18 at 02:00
  • 1
    indeed for python3 the arguments to `print()` need parentheses: `print(tabulate([['Alice', 24], ['Bob', 19]], headers=['Name', 'Age']))` works, see Demo: https://repl.it/repls/FittingAcrobaticExperiment – chickity china chinese chicken Oct 14 '18 at 02:06
  • As for the syntax error message, the Traceback error should point to the exact location or function that is causing the error (look for the `^` caret) symbol pointing to the location. Similar to how the error looks here, `python2` syntax in `python3`: https://repl.it/repls/UsefulFloweryPolygon – chickity china chinese chicken Oct 14 '18 at 02:10

1 Answers1

0

I suspect that you are encountering at least two problems.

  1. You are placing your lists inside square brackets [], which is creating a new list with your list as the first item. Effectively you are making your data 3 dimensional when tabulate expects 2D data. You don't need the extra brackets.
  2. In Python 3, print is a function, so you need to wrap the argument to print in parenthesis

print(tabulate([training_step, train_accuracy, test_accuracy], headers=['Step', 'Train_accuracy', 'Test_accuracy']))

Craig
  • 4,605
  • 1
  • 18
  • 28
  • I tried above command but still not getting tabular form. Actually I want to convert data into table that I can esaily copy on Word or latex – herry Oct 14 '18 at 02:06
  • I can't give you any more help if you don't provide a complete example that will reproduce your problem. – Craig Oct 14 '18 at 02:08