【问题标题】:Urlparse and '\n'urlparse 和 '\n'
【发布时间】:2012-12-14 19:39:27
【问题描述】:

我有:

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”吗?或者我只是错误地使用了这个函数,或者我缺少一些参数/参数?

【问题讨论】:

    标签: python urlparse


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-13
      • 2018-09-22
      • 2018-11-03
      • 1970-01-01
      • 2018-02-09
      相关资源
      最近更新 更多