Python 3特殊字符转义

Python 3特殊字符转义,python,string,html,python-3.x,Python,String,Html,Python 3.x,上面的代码不起作用。它仍在打印“\r\n\t\t\t”内容。有什么建议吗 import urllib from urllib.request import urlopen address='http://www.iitb.ac.in/acadpublic/RunningCourses.jsp?deptcd=EE&year=2012&semester=1' source= urlopen(address).read() source=str(source) from htm

上面的代码不起作用。它仍在打印“\r\n\t\t\t”内容。有什么建议吗

import urllib
from urllib.request import urlopen


address='http://www.iitb.ac.in/acadpublic/RunningCourses.jsp?deptcd=EE&year=2012&semester=1'
source= urlopen(address).read()
source=str(source)


from html.parser import HTMLParser

class MyHTMLParser(HTMLParser):
        def handle_data(self, data):
            x=str(data)
            if x != ('\r\n\t\t\t\t') or ('\r\n\t\t\t\t\t') or ('\r\n\r\n\t\t\t'):
                print("Encountered some data:",x)

parser = MyHTMLParser(strict=False)
parser.feed(source)
应该是

if x != ('\r\n\t\t\t\t') or ('\r\n\t\t\t\t\t') or ('\r\n\r\n\t\t\t')
或者更好:

if x not in ('\r\n\t\t\t\t', '\r\n\t\t\t\t\t', '\r\n\r\n\t\t\t')
您的第一个代码评估为:

if not x.isspace()
请注意,最后一个值作为其自身进行计算!只有空字符串将计算
False
,因此此条件将始终通过

应该是

if x != ('\r\n\t\t\t\t') or ('\r\n\t\t\t\t\t') or ('\r\n\r\n\t\t\t')
或者更好:

if x not in ('\r\n\t\t\t\t', '\r\n\t\t\t\t\t', '\r\n\r\n\t\t\t')
您的第一个代码评估为:

if not x.isspace()

请注意,最后一个值作为其自身进行计算!只有空字符串将计算
False
,因此此条件将始终通过可能是\t和\r等的数量不同,请尝试以下操作:

if (x != ('\r\n\t\t\t\t')) or '\r\n\t\t\t\t\t' or '\r\n\r\n\t\t\t'

可能是\t和\r等的数量不同,请尝试以下操作:

if (x != ('\r\n\t\t\t\t')) or '\r\n\t\t\t\t\t' or '\r\n\r\n\t\t\t'

它仍在打印\r\n\t\t\t\t内容。我不想让它打印出来stuff@sgp如果不是x.strip(),您可能只需检查
,以确保
x
不正确whitespace@jamylak,您的意思是
如果不是x.isspace()
?解释OP编写
如果
语句的方式不正确很重要
('\r\n\t\t\t\t')
('\r\n\r\n\t\t\t')
始终计算为
True
,因此整个语句始终为True。它仍在打印\r\n\t\t\t\t内容。我不想让它打印出来stuff@sgp如果不是x.strip()
,您可能只需检查
,以确保
x
不正确whitespace@jamylak,您的意思是
如果不是x.isspace()
?解释OP编写
如果
语句的方式不正确很重要<代码>('\r\n\t\t\t\t')
('\r\n\t\t')
始终计算为
True
,因此整个语句将始终为True。或仅
x.translate(无),\r\n\t')。strip():
或仅
x.translate(无),\r\n\t')。strip():