0

I would like to print a table on the terminal and for that I use the tabulate package but I have extra return chariot '\n'. How can I remove them?

import pandas as pd
from tabulate import tabulate
df = pd.DataFrame({'schools' : ['Harvard', 'MIT', 'UCal', 'Cornell'],
                   'Faculty' : ['CS', 'Psychology', 'Biology', 'BioInformatics']})
print(tabulate(df,headers='keys',tablefmt='psql'))

Result

('+----+-----------+----------------+\n'
 '|    | schools   | Faculty        |\n'
 '|----+-----------+----------------|\n'
 '|  0 | Harvard   | CS             |\n'
 '|  1 | MIT       | Psychology     |\n'
 '|  2 | UCal      | Biology        |\n'
 '|  3 | Cornell   | BioInformatics |\n'
 '+----+-----------+----------------+')
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Papouche Guinslyzinho
  • 5,277
  • 14
  • 58
  • 101

1 Answers1

1

It looks like you are not actually printing the string, but something else containing the string. In my terminal, if I use your code it works:

jvaubien@ml18-pc03 ~/Downloads> python3 test.py
+----+----------------+-----------+
|    | Faculty        | schools   |
|----+----------------+-----------|
|  0 | CS             | Harvard   |
|  1 | Psychology     | MIT       |
|  2 | Biology        | UCal      |
|  3 | BioInformatics | Cornell   |
+----+----------------+-----------+

With test.py containing your code sample.

But if I do:

import pandas as pd
from tabulate import tabulate
df = pd.DataFrame({'schools' : ['Harvard', 'MIT', 'UCal', 'Cornell'],
                   'Faculty' : ['CS', 'Psychology', 'Biology', 'BioInformatics']})
x = tabulate(df, headers='keys', tablefmt='psql')
print((x,))

I get the same result as you with the parenthesis and the \n.

Are you sure that you print the string and not something else? You have parenthesis around the string which shouldn't be here if you only printed the string.

JimZer
  • 918
  • 2
  • 9
  • 19