Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove \n after "lines.replace"

Tags:

python

Executing the script with the space after "conv:" the results is a "newline" as below:

    lines = lines.replace("www.","conv: ")

conv:
yahoo.com
conv:
yahoo.it
conv:
yahoo.org
conv:
yahoo.net

how remove \n (new line)?

removing the space character after conv: the script runs perfectly.

#!/usr/bin/python

with open('/home/user/tmp/prefix.txt') as f:
    lines = f.read()
    lines = lines.replace("http://","")
    lines = lines.replace("www.","conv:")

    urls = [url.split('/')[0] for url in lines.split()]
    print ('\n'.join(urls))

results is:

conv:yahoo.com
conv:yahoo.it
conv:yahoo.org
conv:yahoo.net

I'd like have:

conv: yahoo.com
conv: yahoo.it
conv: yahoo.org
conv: yahoo.net
like image 957
Marco Rimoldi Avatar asked Sep 26 '22 19:09

Marco Rimoldi


1 Answers

The line

urls = [url.split('/')[0] for url in lines.split()]

splits your lines on a space, so you'll get the conv: part as a single url.

You could do

urls = [url.split('/')[0].replace("conv:", "conv: ") for url in lines.split()]

instead.

like image 121
tynn Avatar answered Oct 11 '22 10:10

tynn