5

この問題に関するスレッドがたくさんあることは知っていますが、問題を解決するスレッドを見つけることができませんでした。

文字列を印刷しようとしていますが、印刷すると特殊文字 (æ、ø、å、ö、ü など) が表示されません。これを使用して文字列を印刷すると、次のrepr()ようになります。

u'Von D\xc3\xbc'u'\xc3\x96berg'

Von Düこれをand に変換する方法を知っている人はいますÖbergか? これらの文字が無視されないことが重要ですmyStr.encode("ascii", "ignore")

編集

これは私が使用するコードです。BeautifulSoup を使用して Web サイトをスクレイピングしています。<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
4

3 に答える 3

12

治療よりも予防​​が大切です。必要なのは、そのゴミがどのように作られているかを知ることです。質問を編集して、それを作成するコードを表示してください。修正をお手伝いします。誰かがやったようです:

your_unicode_string =  original_utf8_encoded_bytestring.decode('latin1')

解決策は、単純にプロセスを逆にしてからデコードすることです。

correct_unicode_string = your_unicode_string.encode('latin1').decode('utf8')

更新あなたが提供したコードに基づいて、考えられる原因は、Web サイトがエンコードされていると宣言しているISO-8859-1(aka latin1) が、実際には UTF-8 でエンコードされていることです。質問を更新して、URL を表示してください。

表示できない場合は、BS docsを読んでください。使用する必要があるようです:

BeautifulSoup(web, from_encoding='utf8')
于 2012-04-02T10:11:34.783 に答える
3

多くの言語でのUnicodeサポートは混乱を招くため、ここでのエラーは理解できます。uこれらの文字列はUTF-8バイトであり、先頭にをドロップすると正しく機能します。

>>> err = u'\xc3\x96berg'
>>> print err
Ã?berg
>>> x = '\xc3\x96berg'
>>> print x
Öberg
>>> u = x.decode('utf-8')
>>> u
u'\xd6berg'
>>> print u
Öberg

詳細については、以下をご覧ください。

http://www.joelonsoftware.com/articles/Unicode.html

http://docs.python.org/howto/unicode.html


先に進む前に、これらのリンクを実際に読んで、何が起こっているのかを理解する必要があります。ただし、今日絶対に機能するものが必要な場合は、私が恥ずかしいこの恐ろしいハックを使用して、公開することができます。

def convert_fake_unicode_to_real_unicode(string):
    return ''.join(map(chr, map(ord, string))).decode('utf-8')
于 2012-04-02T09:25:58.780 に答える
1

文字列の内容はUnicodeではなく、UTF-8でエンコードされています。

>>> print u'Von D\xc3\xbc'
Von Dü
>>> print 'Von D\xc3\xbc'
Von Dü

>>> print unicode('Von D\xc3\xbc', 'utf-8')
Von Dü
>>> 

編集:

>>> print '\xc3\x96berg' # no unicode identifier, works as expected because it's an UTF-8 encoded string
Öberg
>>> print u'\xc3\x96berg' # has unicode identifier, means print uses the unicode charset now, outputs weird stuff
Ãberg

# Look at the differing object types:
>>> type('\xc3\x96berg')
<type 'str'>
>>> type(u'\xc3\x96berg')
<type 'unicode'>

>>> '\xc3\x96berg'.decode('utf-8') # this command converts from UTF-8 to unicode, look at the unicode identifier in the output
u'\xd6berg'
>>> unicode('\xc3\x96berg', 'utf-8') # this does the same thing
u'\xd6berg'
>>> unicode(u'foo bar', 'utf-8') # trying to convert a unicode string to unicode will fail as expected
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: decoding Unicode is not supported
于 2012-04-02T09:27:34.943 に答える