Python 带“0”的普通字符串;xe2x80x93“&引用-&引用;烧焦

Python 带“0”的普通字符串;xe2x80x93“&引用-&引用;烧焦,python,python-3.x,string,unicode-escapes,Python,Python 3.x,String,Unicode Escapes,我对python3中的字符串有问题。我的varg是一个普通字符串。但其中有一个恼人的“xe2x80x93”,因为它来自web解析器。我想将其转换为合适的字符“-” content = str(urllib.request.urlopen(site, timeout=10).read()) g = content.split('<h1 itemprop="name"')[1].split('</span></h1>')[0].split('<span>')

我对python3中的字符串有问题。我的varg是一个普通字符串。但其中有一个恼人的“xe2x80x93”,因为它来自web解析器。我想将其转换为合适的字符“-”

content = str(urllib.request.urlopen(site, timeout=10).read())
g = content.split('<h1 itemprop="name"')[1].split('</span></h1>')[0].split('<span>')[1].replace("\\", "")

print(type(g)) --> string
print(g)  --> "Flash xe2x80x93 der rote Blitz"

print(g.encode('latin-1').decode('utf-8')) --> AttributeError: 'str' object has no attribute 'decode'
print(repr(g.decode('unicode-escape'))) --> AttributeError: 'str' object has no attribute 'decode'
print(g.encode('ascii','replace')) --> b'Flash xe2x80x93 der rote Blitz'
print(bytes(g, "utf-8").decode()) --> "Flash xe2x80x93 der rote Blitz"
print(bytes(g, "utf-8").decode("unicode_escape")) --> "Flash â der rote Blitz"
content=str(urllib.request.urlopen(site,timeout=10.read())

g=content.split(“您对
decode
的想法是正确的

通过将输出包装在这一行的
str(…)

content = str(urllib.request.urlopen(site, timeout=10).read())
您可以将bytes对象转换为字符串(在
内容
中的前导
b'
和尾随
'
),或者,如果它已被解码为ISO-8859-1,则不执行任何操作

无论哪种情况,都不要这样做——删除包装
str
调用

现在,内容将是一个
bytes
对象或
str
对象

因此,如果它是一个字符串,它将被(错误地)解码为ISO-8859-1。您需要将其编码回字节对象,然后正确解码:

content = urllib.request.urlopen(site, timeout=10).read()

if isinstance(content, str):
    content = content.encode('iso-8859-1')
content = content.decode('utf8')
现在,您的
\xe2\x80\x93
字节应正确显示为:–

更新

根据您的评论,您需要做的只是:

content = urllib.request.urlopen(site, timeout=10).read().decode('utf8')

print(type(urllib.request.urlopen(site,timeout=10.read())
say是什么意思?),所以我不得不用“.decode”(“utf-8”)”,而不是str()?谢谢!我已经更新了我的答案,给你一个应该适合你的替换的第一行。卢卡斯,是的——试试看,让我/我们知道它是怎么回事:)是的@犹太矮人。非常感谢你!