簡體   English   中英

ascii 編解碼器無法解碼

[英]ascii codec can't decode

我在嘗試打開目錄中的所有 .txt 文件時遇到錯誤,當我的目錄中只有 1 個 txt 文件時,我的代碼可以工作,否則會彈出此消息:

Traceback (most recent call last):
  File "/Users/Name/Desktop/TCSS 142/Project 2/project2.py", line 17, in <module>
for line in file:
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/encodings/ascii.py", line 26, in decode
   return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0x92 in position 3: ordinal not in range(128)

這是我的代碼:

import glob

# Returns a list of all filenames ending in .txt
# precondition: none
# postcondition: a list of all filenames in the current directory
#                with a .txt extension
def getFilesInDir():
    filenames = glob.glob('./*.txt')
    for i in range(len(filenames)):
        filenames[i] = filenames[i][2:]
    return filenames

files = getFilesInDir()

for el in files:
    file = open(el, 'r')
    for line in file:
        print(line)
    file.close()

好的,@Evert 的評論解決了我的問題,當我打開文件時,我的 open 語句如下所示:

file = open(el, 'r', encoding = 'cp1252')

您的默認編碼(用於讀取文件)似乎是 ASCII; 它無法讀取的字符似乎是(Windows)智能引號(“卷曲”撇號' )。

為了能夠讀取文件,您需要指定其編碼。 我不認為0x92是有效的 UTF 代碼點(但我可能錯了); 由於它是 Windows 智能報價,請嘗試使用Windows 拉丁字母編碼cp1252

file = open(el, 'r', encoding = 'cp1252')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM