我一直在努力解决这个问题。我正在尝试将字符串写入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"
我希望你能解释为什么会这样,以及我如何解决。提前谢谢!
答案 0 :(得分:21)
\ xc2 \ xa0表示 0xC2 0xA0 是所谓的
不间断的空间
它是UTF-8编码中的一种不可见的控制字符。有关它的更多信息,请查看维基百科:https://en.wikipedia.org/wiki/Non-breaking_space
我复制了你在问题中粘贴的内容并获得了预期的输出。