首页 > 解决方案 > 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 也可以这样做吗?

标签: pythonpandasjupyter-notebookspecial-characters

解决方案


感谢@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

推荐阅读