以在“conv:”后加空格的方式执行脚本,结果是一个“换行符”,如下所示:
lines = lines.replace("www.","conv: ")
conv:
yahoo.com
conv:
yahoo.it
conv:
yahoo.org
conv:
yahoo.net
如何去除换行符(\n)?
在转换后删除空格字符:脚本将完美运行。
#!/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))
结果如下:
conv:yahoo.com
conv:yahoo.it
conv:yahoo.org
conv:yahoo.net
我想要:
conv: yahoo.com
conv: yahoo.it
conv: yahoo.org
conv: yahoo.net
lines = lines.replace("www.","conv: ".rstrip())
。 - Stefano Saittaurls = [url.split('/')[0] for url in lines.split('\n')]
- ssm