我知道有很多关于这个问题的帖子,但我还没有找到解决我的问题的帖子。
我正在尝试打印一个字符串,但是当打印时它不显示特殊字符(例如æ,ø,å,ö和ü)。 当我使用repr()
打印字符串时,得到以下结果:
u'Von D\xc3\xbc'
和 u'\xc3\x96berg'
有人知道如何将其转换为 Von Dü
和 Öberg
吗?对我来说,重要的是这些字符不被忽略,例如myStr.encode("ascii", "ignore")
。
编辑
这是我用来爬取网站的代码。表格(<table>
)中单元格(<td>
)的内容被放入变量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