フランス語と英語の文字を含む単語を含むファイルからデータを読み取っています。考えられるすべての英語とフランス語の文字 (文字列として保存) のリストを作成しようとしています。私は以下のコードでこれを行います:
# encoding: utf-8
def trackLetter(letters, line):
for a in line:
found = False;
for b in letters:
if b==a:
found = True
if not found:
letters += a
cur_letters = []; # for storing possible letters
data = urllib2.urlopen('https://duolinguist.wordpress.com/2015/01/06/top-5000-words-in-french-wordlist/', 'utf-8')
for line in data:
trackLetter(cur_letters, line)
# works if I print here
print cur_letters
このコードは以下を出力します。
['t', 'h', 'e', 'o', 'f', 'a', 'n', 'd', 'i', 'r', 's', 'b', ' y'、'w'、'u'、'm'、'l'、'v'、'c'、'p'、'g'、'k'、'x'、'j'、'z' , 'q', '\xc3', '\xa0', '\xaa', '\xb9', '\xa9', '\xa8', '\xb4', '\xae', '-', ' \xe2', '\x80', '\x99', '\xa2', '\xa7', '\xbb', '\xaf']
私が UTF エンコーディングを指定したにも関わらず、ASCII へのある種の変換で明らかにフランス語の文字が失われました! 奇妙なことに、行を直接印刷すると (コメントとして表示されます)、フランス語の文字が完全に表示されます!
é, è, ê, etc.
これらの文字 ( ) を保持したり、元のバージョンに戻すにはどうすればよいですか?