Python HTML Encoding\xc2\xa0

Sam*_*rry 13 html python encoding

我一直在努力解决这个问题.我正在尝试将字符串写入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
Run Code Online (Sandbox Code Playgroud)

输出:

"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"
Run Code Online (Sandbox Code Playgroud)

期望的输出:

"Grocery giant and household name Woolworths is battered and bruised. 
But behind the problems are still the makings of a formidable company"
Run Code Online (Sandbox Code Playgroud)

我希望你能解释为什么会这样,以及我如何解决.提前致谢!

liu*_*yix 24

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

不间断的空间

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

我复制了你在问题中粘贴的内容并获得了预期的输出.

  • 谢谢.这解决了它.我内置:x.replace("\ xc2\xa0","") (7认同)