0

我有:

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='')

它是否正确?解析期间不应该删除“\n”吗?或者我只是错误地使用了这个函数,或者我错过了一些参数/参数?

4

1 回答 1

2

解析文本行时,始终使用str.rstrip()删除尾随 CRLF。这也将对您的情况有所帮助:

for line in open('file.txt'):
    line = line.rstrip()  # strip the trailing CRLF
    # do what you need with the line
于 2012-12-14T11:33:43.280 回答