2016-09-26 1 views
0

비 인코딩 된 유니 코드를 포함하는 텍스트 파일 목록 목록이 있습니다. 파이썬은 텍스트 파일에서 직접 문을 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'] 

==> 
관련 문제