2016-09-26 30 views
0

私は、非エンコードのユニコードを含むテキストファイルのリストを持っています。 Pythonは、テキストファイルから直接json.loads()文のようなものを何も実行せずに、リストとして読み込まれているオブジェクトを認識しています。テキストファイルからリストのリストを読み込み、結合してデコードする

テキストファイルのすべての行を読み込んだ後、forループを実行して各サブリストを繰り返し処理すると、何も起こりません。 JSONにロードしようとする前にオブジェクト全体を最初にエンコードしようとすると、何も起こりません。

import glob 

myglob = 'C:\\mypath\\*.txt' 
myglob = ''.join(myglob) 

for name in glob.glob(myglob): 

    split_name = name.split('\\')[5] 

    with open(name) as f: 

     content = f.readlines() 

     for xx in content: 

      print xx.encode('utf-8') 

入力データは次のようになります:出力は、エンコードされた上記の内容を文字列の3行でなければなりません

[['Alexis S\xe1nchez', 'Alexis', 'S\xe1nchez', 'Forward', 'Arsenal', '13', '25244'],['H\xe9ctor Beller\xedn', 'H\xe9ctor', 'Beller\xedn', 'Defender', 'Arsenal', '13', '125211'],['Libor Koz\xe1k', 'Libor', 'Koz\xe1k', 'Forward', 'Aston Villa', '24', '67285']] 

は、ここに私のコードです。誰かが私が間違っていることを教えてもらえますか?

おかげ

答えて

0

たぶん、次のコードスニペットのようなもの?

listInput = [['Alexis S\xe1nchez', 'Alexis', 'S\xe1nchez', 'Forward', 'Arsenal', '13', '25244'], 
    ['H\xe9ctor Beller\xedn', 'H\xe9ctor', 'Beller\xedn', 'Defender', 'Arsenal', '13', '125211'], 
    ['Libor Koz\xe1k', 'Libor', 'Koz\xe1k', 'Forward', 'Aston Villa', '24', '67285']] 

for listItem in listInput: 

    for aItem in listItem: 
     aItem = aItem.encode('utf-8') 

    print (listItem) 

出力

==> python D:\test\Python\39708736.py 
['Alexis Sánchez', 'Alexis', 'Sánchez', 'Forward', 'Arsenal', '13', '25244'] 
['Héctor Bellerín', 'Héctor', 'Bellerín', 'Defender', 'Arsenal', '13', '125211'] 
['Libor Kozák', 'Libor', 'Kozák', 'Forward', 'Aston Villa', '24', '67285'] 

==> 
関連する問題