Python HTML编码\xc2\xa0 [英] Python HTML Encoding \xc2\xa0
问题描述
我已经为此苦苦挣扎了一段时间。我正在尝试将字符串写入HTML,但是一旦清理它们,格式就会出现问题。下面是一个示例:
I've been struggling with this one for a while. I'm trying to write strings to HTML but have issues with the format once I've cleaned them. Here's an example:
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"
我希望您能够解释这种情况的发生原因以及如何解决。
I'm hoping you're able to explain why this happens and how I can fix. Thanks in advance!
推荐答案
\xc2\xa0表示 0xC2 0xA0 就是所谓的
\xc2\xa0 means 0xC2 0xA0 is so-called
不间断空格
Non-breaking space
一种UTF-8编码中的不可见控制字符。有关它的更多信息,请检查Wikipedia: https://en.wikipedia.org/wiki/Non-breaking_space
It is a kind of invisible control character in UTF-8 encodings. More info about it check the wikipedia: https://en.wikipedia.org/wiki/Non-breaking_space
我复制了您在问题中粘贴的内容,并获得了预期的输出。
I copied what you have pasted in the questions and got the expected output.
这篇关于Python HTML编码\xc2\xa0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!