Search code examples
pythonurlparse

Urlparse and '\n'


I have:

from urlparse import urlparse
s = "http://google.com" + "\n" # this line is read from file, when I loop over file's lines 
urlparse(s)
ParseResult(scheme='http', netloc='google.com\n', path='', params='', query='', fragment='')

Is this correct? Shouldn't "\n" be removed during parsing? Or I'm just using this function incorrectly or I'm missing some argument/parameter?


Solution

  • When parsing text lines, always use str.rstrip() to remove the trailing CRLF. This will also help you in your case:

    for line in open('file.txt'):
        line = line.rstrip()  # strip the trailing CRLF
        # do what you need with the line