交作业
Topic source@廖雪峰老师 有一个小问题,为什么例子里这几行代码貌似没有起到作用?
from html.entities import name2codepoint
def handle_entityref(self, name):
print('&%s;' % name)
def handle_charref(self, name):
print('&#%s;' % name)
parser.feed('''<html>
<head></head>
<body>
<!-- test html parser -->
<p>Some <a href=\"#\">html</a> HTML
tutorial...<br>END</p>
</body></html>''')
这里的
并没有打印出来呀,依旧是空格?
好像根本就没有调用handle_entityref()
def handle_entityref(self, name):
name = chr(name2codepoint[name])
print('&%s;' % name)
def handle_charref(self, name):
if name.startswith('x'):
name = chr(int(name[1:], 16))
else:
name = chr(int(name))
print('&#%s;' % name)
```
把这段代码插入到对应的地方,应该就可以了。
请问大神在win下运行得到UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 这种错误怎么办
win下运行得到UnicodeEncodeError: 'gbk'...是因为cmd(或PowerShell)用的编码方式是GBK,这种方式无法对输出的一些字符进行编码。解决方法是在程序中先自我编码: print(data.encode('GB18030')) 或 print(data.encode('GBK','ignore'))
这个是print()函数自身有限制,不能完全打印所有的unicode字符。 在IDLE下运行就不会有这个问题。 或者改一下python的默认编码成'utf-8'就行了
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')
- 1
LesLieM樂
输出为: