Remove \ n after "lines.replace"

Running the script with a space after "conv:" the results are a "new line", as shown below:

lines = lines.replace("www.","conv: ") conv: yahoo.com conv: yahoo.it conv: yahoo.org conv: yahoo.net 

how to remove \ n (new line)?

removing the space character after conv: script works fine.

 #!/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)) 

result:

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

I would like to:

 conv: yahoo.com conv: yahoo.it conv: yahoo.org conv: yahoo.net 
+5
source share
4 answers

Line

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

breaks your lines into spaces, so you get the conv: part as a single url.

You could do

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

instead.

+2
source

You do not even need to understand the list:

 #!/usr/bin/python with open('/home/user/tmp/prefix.txt') as f: lines = f.read() lines = lines.replace("http://","") lines = lines.replace("www.","conv: ") print lines 
+1
source

The easiest way to accomplish your goal from your given code is to change

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

to

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

Thus, you divide the lines into lines of a new line, and not all spaces.

+1
source

Why are you joining fingerprints with \ n while at this point you will have something like urls = ['conv:', 'yahoo.com']. So print ('.join (urls)) will work will go to the next line (I don’t know the input file assuming one URL per line)

0
source

Source: https://habr.com/ru/post/1239430/


All Articles