Jupyter笔记本新行'\ n'不会像在文本编辑器中那样被解释

问题描述 投票:1回答:1

我有两个人之间的对话数据,他们有时间说话,有个人名字作为用户,还有一个包含他们讲话内容的脚本列。出于可读性考虑,我只想将它们加入换行符,而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笔记本也可以这样做吗?

python pandas jupyter-notebook special-characters
1个回答
0
投票

感谢@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
© www.soinside.com 2019 - 2024. All rights reserved.