简体   繁体   English

Python 换行 \n 在 jupyter 笔记本中不起作用

[英]Python Newline \n not working in jupyter notebooks

I'm trying to display the tuples of a postgreSQL table neatly in my Jupyter Notebook, but the newline \n escape character doesn't seem to work here (it works for my python scripts w/ same code outside of jupyter).我试图在我的 Jupyter Notebook 中整齐地显示 postgreSQL 表的元组,但换行符 \n 转义字符似乎在这里不起作用(它适用于我的 python 脚本,在 jupyter 之外具有相同的代码)。

I'm trying to run:我正在尝试运行:

cur.execute('SELECT * FROM Cars')
'\n '.join(str(x) for x in cur.fetchall())

But my output still contains the '\n' characters themselves:但是我的 output 本身仍然包含 '\n' 字符:

"(1, 'Toyota', 'Supra', 2020, 'Sport', 'Gas', 49995.0, 7, 280.0, datetime.date(2020, 5, 27), 'Loan', 50150.0, 300.0, 987654321, 333356789)\n (4, 'Chevrolet', 'Corvette', 2020, 'Sport', 'Gas', 55999.0, 4, 280.0, datetime.date(2020, 5, 27), 'Loan', 58999.0, 300.0, 987444321, 333356789)\n (2, 'Toyota', '4Runner', 2018, 'Sport', 'Gas', 40599.0, 13266, 280.0, datetime.date(2020, 5, 27), 'Loan', 58999.0, 300.0, 987334321, 333356789)"

Any ideas as to what I need to do or add?关于我需要做什么或添加的任何想法?

When you don't put the output in print statement.当您不将 output 放在打印语句中时。 "\n" would be printed as "\n" instead of newline in jupyter notebook and python shell.在 jupyter notebook 和 python shell 中,“\n”将打印为“\n”而不是换行符。

in: 'A\nB'在: 'A\nB'

out: 'A\nB'出: 'A\nB'

in: print('A\nB')在: print('A\nB')

out:出去:

A
B

The solution you need is: print('\n '.join(str(x) for x in cur.fetchall()))您需要的解决方案是: print('\n '.join(str(x) for x in cur.fetchall()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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