Python显示特殊字符

标签 python encoding special-characters python-unicode

我知道有很多关于这个问题的话题,但我还没有找到能解决我的问题的话题。

我正在尝试打印一个字符串,但在打印时它不显示特殊字符(例如 æ、ø、å、ö 和 ü)。当我使用 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/

相关文章:

php - 如何删除所有特殊字符,如 "RS"(记录分隔符)

Python Pandas 数据帧移位在应用函数中不起作用

python - 查找命名空间内的所有元素

python - 为类属性生成随机数

c++ - 为什么字符串有时朝一个方向写,有时朝另一个方向写?

python - 将 html 实体转换为 python 中的值

python - python代码对象和抽象语法树有什么关系?

c - ruby 内部如何表示字符串?

python - 解决 ascii codec can't decode byte in position ordinal not in range

c++ - 在 C/C++ 中使用 `$` 作为标识符是否安全?