2017-01-06 1 views
1

.txt 파일을 열고 읽으려고합니다. 많은 양의 텍스트가 포함되어 있습니다. 아래 코드는,이 문제를 해결하는 방법을 모른다. 어떤 도움을 주시면 감사하겠습니다. 내가 .txt 파일을 입력 할 때codecs.ascii_decode (input, self.errors) [0] UnicodeDecodeError : 'ascii'코덱은 318의 바이트 0xc2를 디코딩 할 수 없습니다. 서수가 범위 내에 없습니다 (128)

file = input("Please enter a .txt file: ") 
myfile = open(file) 
x = myfile.readlines() 
print (x) 

이이 전체 오류 메시지가 아래에 표시됩니다되어

line 10, in <module> x = myfile.readlines() 
line 26, in decode return codecs.ascii_decode(input, self.errors)[0] 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 318: ordinal not in range(128) 
+0

다음을 참조하십시오. http://stackoverflow.com/questions/491921/unicode-utf-8-reading-and-writing-to-files-in-python –

+0

@AndriiAbramov 그 질문에서 파일 UTF-8로 인코딩되었습니다. 그게 여기에 해당하는지 모르겠다. 그렇지 않다면 많은 좌절을 가져올 것이다. –

답변

-1

@AndriiAbramamov은 권리, 당신의 그 질문을 확인 갈까요, 여기 당신이 열 수있는 방법은 당신의 링크

import codecs 
f = codecs.open('words.txt', 'r', 'UTF-8') 
for line in f: 
    print(line) 

에도 또 다른 방법이다 파일은 정규식을 사용하는 것입니다, 그래서 당신이 파일을 열 때 등등 따옴표와 같은 특수 문자를 제거 할 수 있습니다.

def test(): 
    path = './test.log' 
    file = open(path, 'r+', encoding='utf-8') 
    while True: 
     lines = file.readlines() 
     if not lines: 
      break 
     for line in lines: 
      print(line) 

당신은 정확하게 인코딩 PARAM을 부여해야 대신 코덱을 사용

관련 문제

 관련 문제