【问题标题】:Python HTML Encoding \xc2\xa0Python HTML 编码 \xc2\xa0
【发布时间】:2015-12-01 21:07:47
【问题描述】:

我已经为此苦苦挣扎了一段时间。我正在尝试将字符串写入 HTML,但是一旦我清理了它们,格式就会出现问题。这是一个例子:

paragraphs = ['Grocery giant and household name Woolworths is battered and bruised. ', 
'But behind the problems are still the makings of a formidable company']

x = str(" ")
for item in paragraphs:
    x = x + str(item)
x

输出:

"Grocery giant and household name\xc2\xa0Woolworths is battered and\xc2\xa0bruised. 
But behind the problems are still the makings of a formidable\xc2\xa0company"

期望的输出:

"Grocery giant and household name Woolworths is battered and bruised. 
But behind the problems are still the makings of a formidable company"

我希望您能够解释为什么会发生这种情况以及我该如何解决。提前致谢!

【问题讨论】:

  • 您是否检查过源字符串中的异常 Unicode 空白?

标签: python html encoding


【解决方案1】:

\xc2\xa0 表示 0xC2 0xA0 就是所谓的

不间断空格

它是UTF-8编码中的一种不可见的控制字符。有关它的更多信息,请查看维基百科:https://en.wikipedia.org/wiki/Non-breaking_space

我复制了您在问题中粘贴的内容并得到了预期的输出。

【讨论】:

  • 谢谢。这解决了它。我内置:x.replace("\xc2\xa0", "")
猜你喜欢
  • 2018-02-03
  • 1970-01-01
  • 2021-04-18
  • 2016-02-25
  • 1970-01-01
  • 1970-01-01
  • 2017-05-30
  • 2020-10-05
  • 2016-09-04
相关资源
最近更新 更多