我知道有很多关于这个问题的话题,但我还没有找到能解决我的问题的话题。
我正在尝试打印一个字符串,但在打印时它不显示特殊字符(例如 æ、ø、å、ö 和 ü)。当我使用 repr()
打印字符串时这是我得到的:
u'Von D\xc3\xbc'
和 u'\xc3\x96berg'
有谁知道如何将其转换为 Von Dü
和 Öberg
?对我来说重要的是这些字符不会被忽略,例如myStr.encode("ascii", "ignore")
.
编辑
这是我使用的代码。我使用 BeautifulSoup 来抓取网站。表格 ( <td>
) 中单元格 ( <table>
) 的内容被放入变量 name
中.这是包含我无法打印的特殊字符的变量。
web = urllib2.urlopen(url);
soup = BeautifulSoup(web)
tables = soup.find_all("table")
scene_tables = [2, 3, 6, 7, 10]
scene_index = 0
# Iterate over the <table>s we want to work with
for scene_table in scene_tables:
i = 0
# Iterate over < td> to find time and name
for td in tables[scene_table].find_all("td"):
if i % 2 == 0: # td contains the time
time = remove_whitespace(td.get_text())
else: # td contains the name
name = remove_whitespace(td.get_text()) # This is the variable containing "nonsense"
print "%s: %s" % (time, name,)
i += 1
scene_index += 1
最佳答案
预防胜于治疗。你需要的是找出这些垃圾是如何产生的。请编辑您的问题以显示创建它的代码,然后我们可以帮助您修复它。看起来有人做过:
your_unicode_string = original_utf8_encoded_bytestring.decode('latin1')
解决方法是简单地反转这个过程,然后解码。
correct_unicode_string = your_unicode_string.encode('latin1').decode('utf8')
更新 根据您提供的代码,可能的原因是该网站声明其编码为 ISO-8859-1
(又名 latin1
) 但实际上它是用 UTF-8 编码的。请更新您的问题以向我们显示网址。
如果不能显示,请阅读the BS docs ;看起来你需要使用:
BeautifulSoup(web, from_encoding='utf8')
关于Python显示特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9973815/