Python换行符\ n在jupyter笔记本中不起作用 [英] Python Newline \n not working in jupyter notebooks

查看:508
本文介绍了Python换行符\ n在jupyter笔记本中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Jupyter笔记本中整齐地显示postgreSQL表的元组,但是换行符\ n转义字符似乎在这里不起作用(它适用于我的python脚本,在jupyter之外具有相同代码).

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

我正在尝试运行:

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

但是我的输出本身仍然包含'\ n'字符:

But my output still contains the '\n' characters themselves:

"(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?

推荐答案

不将输出放入print语句时."\ n"将被打印为"\ n"而不是在jupyter笔记本和python shell中使用换行符.

When you don't put the output in print statement. "\n" would be printed as "\n" instead of newline in jupyter notebook and python shell.

在:'A \ nB'

输出:'A \ nB'

在: print('A \ nB')

退出:

A
B

您需要的解决方案是: print('\ n'.join(str(x)for cur.fetchall()中的x))

The solution you need is: print('\n '.join(str(x) for x in cur.fetchall()))

这篇关于Python换行符\ n在jupyter笔记本中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆