我知道关于这个问题有很多线程,但我还没有找到一个可以解决我的问题的线程。
我正在尝试打印一个字符串,但打印时它不显示特殊字符(例如æ、ø、å、ö 和 ü)。当我使用它打印字符串时repr()
,我得到:
u'Von D\xc3\xbc'
和u'\xc3\x96berg'
有谁知道我如何将其转换为Von Dü
and Ö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