我想在航站楼上打印一个表格,为此我使用了tabulate软件包,但我有额外的返程车辆'\n‘。如何删除它们?
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'))结果
('+----+-----------+----------------+\n'
'| | schools | Faculty |\n'
'|----+-----------+----------------|\n'
'| 0 | Harvard | CS |\n'
'| 1 | MIT | Psychology |\n'
'| 2 | UCal | Biology |\n'
'| 3 | Cornell | BioInformatics |\n'
'+----+-----------+----------------+')发布于 2019-06-13 05:10:18
看起来您实际上并没有打印字符串,而是打印了包含该字符串的其他内容。在我的终端中,如果我使用你的代码,它可以工作:
jvaubien@ml18-pc03 ~/Downloads> python3 test.py
+----+----------------+-----------+
| | Faculty | schools |
|----+----------------+-----------|
| 0 | CS | Harvard |
| 1 | Psychology | MIT |
| 2 | Biology | UCal |
| 3 | BioInformatics | Cornell |
+----+----------------+-----------+其中的test.py包含您的代码示例。
但如果我这么做了:
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获得与您使用括号和\n相同的结果。
您确定打印的是字符串而不是其他内容吗?在字符串两边有括号,如果只打印字符串,就不应该出现在这里。
https://stackoverflow.com/questions/56569250
复制相似问题