Puneet Sinha
Puneet Sinha

Reputation: 1081

Jupyter notebook new line '\n' is not interpreted as in text editor

I have the data of a conversation between two people where I have time when they spoke something, name of person as user and a script column containing what they spoke. For readability I just wanted to join them with new line and Jupyter notebook doesn't read '\n' as it is.

My dataframe looks like this:

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

Now the whole conversation between them looks like this:

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'

I am looking for output like:

0:ram:hi
1:ram:there
2:neha:ball
3:neha:item
4:neha:go there
5:neha:kk
6:neha:hshs

If i copy paste this in editor, it gives me the desired output... Can jupyter notebook also do the same?

Upvotes: 1

Views: 1959

Answers (1)

Puneet Sinha
Puneet Sinha

Reputation: 1081

Thanks to @luigigi

print(script)

is giving desired output

0:ram:hi
1:ram:there
2:neha:ball
3:neha:item
4:neha:go there
5:neha:kk
6:neha:hshs

Upvotes: 1

Related Questions