python - Jupyter notebook 新行 '\n' 不解释为在文本编辑器中
问题描述
我有两个人之间的对话数据,我有时间在他们说话时,作为用户的人名和包含他们所说内容的脚本列。为了便于阅读,我只想用新行加入它们,而 Jupyter 笔记本并没有按原样读取 '\n' 。
我的数据框如下所示:
import numpy as np
import random
df = pd.DataFrame(dict(
script = ['hi', 'there', 'ball', 'item', 'go there', 'kk','hshs', 'ggsgs', 'bye', 'bye'],
user = np.random.choice(['neha', 'ram'], 10, replace=True),
time = range(0,10)))
df['conv'] = df[['time','user','script']].apply(lambda x: ':'.join(x.astype(str)), axis=1)
# df :
script user time conv
0 hi neha 0 0:neha:hi
1 there ram 1 1:ram:there
2 ball neha 2 2:neha:ball
3 item neha 3 3:neha:item
4 go there ram 4 4:ram:go there
5 kk ram 5 5:ram:kk
6 hshs neha 6 6:neha:hshs
7 ggsgs neha 7 7:neha:ggsgs
8 bye neha 8 8:neha:bye
9 bye neha 9 9:neha:bye
现在他们之间的整个对话是这样的:
script='\n'.join(df.conv)
script
'0:ram:hi\n1:ram:there\n2:neha:ball\n3:neha:item\n4:neha:go there\n5:neha:kk\n6:neha:hshs\n7:ram:ggsgs\n8:ram:bye\n9:ram:bye'
我正在寻找如下输出:
0:ram:hi
1:ram:there
2:neha:ball
3:neha:item
4:neha:go there
5:neha:kk
6:neha:hshs
如果我将其复制粘贴到编辑器中,它会给我所需的输出... jupyter notebook 也可以这样做吗?
解决方案
感谢@luigigi
print(script)
正在提供所需的输出
0:ram:hi
1:ram:there
2:neha:ball
3:neha:item
4:neha:go there
5:neha:kk
6:neha:hshs
推荐阅读
- jquery - 添加块表单对话框中的jQuery字体选择器提交表单而不是选择字体
- python - Numpy分段函数在scipy quad中很慢
- swift - Swift - 修改变量
- spring-boot - 如何为 Thymeleaf 激活不同的日志记录级别?
- c++ - 检查 Boost Interprocess managed_shared_memory 是否已经存在?
- bash - 使用 bash 4 语法将 Makefile 中字符串的首字母大写
- matlab - 如何找到 R、G、B、H、S、V 值
- r - 是否可以从使用 `facet_wrap` 的 `ggplots` 中获取`轴中断`?
- javascript - 动态地将变量名称添加到 json 数据中
- google-analytics - 谷歌分析在 4 月 25 日停止了对某些页面的工作