python - 寻找 html.unescape ("&nbsp")

标签 python python-3.x

<分区>

这是我的代码:

import os
import html

a = html.unescape("home&nbsp;-&nbsp;study")
b = "test"
print(a)
s = (a, b)
print(s)

这是我的结果:

home - study
('home\xa0-\xa0study', 'test')

为什么打印出来的结果是这样的?

最佳答案

默认情况下,打印像 tuples 这样的容器, lists和其他人 将使用 repr他们的元素(在 CPython 中,它被选择不实现 <container>.__str__ 而是让 object.__str__ 填充它的位置。__str__object 然后将调用 tuple.__repr__ 然后继续调用 the repr of the elements it contains 。见PEP 3140 更多。)

调用 repr对于带有转义码(例如 \xa0 )的字符串,实际上不会转义它们:

print(repr(a))
'home\xa0-\xa0study'

要进一步验证,请尝试 print(s[0]) .通过提供 str物体就位 0 python 将直接调用 its __str__并正确转义十六进制。

关于python - 寻找 html.unescape ("&nbsp"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39939679/

相关文章:

python - 是否有另一种方法可以用白色填充旋转图像外部的区域? 'fillcolor' 不适用于旧版本的 Python

python - 在 Fabric 中,如何从另一个 python 文件执行任务?

java - 检查字符串是否包含 char 数组中的值

python - 为什么 multiprocessing.Pool.map 与我的自定义部分函数一起挂起?

python-3.x - 输入维度 Tensorflow v1.8 ConvLSTMCell

python-3.x - 如何添加快捷方式来运行项目中的所有单元测试(ST3 + Anaconda)?

python - 在python中的字符串中的任何元音之前或之后立即删除 "y"

python - CommandError : 'learning_log' s not a valid project name. 请确保名称是有效的标识符

python - 根据Python中另一个数据帧的行值从一个数据帧中获取列?

python - 如何为 PyQt 安装新的 QStyle?