簡體   English   中英

Python從文件編碼問題中讀取

[英]Python reading from file encoding problem

當我這樣讀,有些文件

list_of_files = glob.glob('./*.txt') # create the list of files
for file_name in list_of_files:
    FI = open(file_name, 'r', encoding='cp1252')

錯誤:

UnicodeDecodeError:“ charmap”編解碼器無法解碼位置1260的字節0x9d:字符映射到

當我切換到這個

list_of_files = glob.glob('./*.txt') # create the list of files
for file_name in list_of_files:
    FI = open(file_name, 'r', encoding="utf-8")

錯誤:

UnicodeDecodeError:“ utf-8”編解碼器無法解碼位置1459中的字節0x92:無效的起始字節

我已經讀過我應該以二進制文件形式打開它。 但我不知道該怎么做。 這是我的功能:

def readingAndAddToList():
    list_of_files = glob.glob('./*.txt') # create the list of files
    for file_name in list_of_files:
        FI = open(file_name, 'r', encoding="utf-8")
        stext = textProcessing(FI.read())# split returns a list of words delimited by sequences of whitespace (including tabs, newlines, etc, like re's \s)
        secondaryWord_list = stext.split()
        word_list.extend(secondaryWord_list) # Add words to main list
        print("Lungimea fisierului ",FI.name," este de", len(secondaryWord_list), "caractere")
        sortingAndNumberOfApparitions(secondaryWord_list)
        FI.close()

只是我的函數的開始很重要,因為我在閱讀部分遇到了錯誤

如果您在Windows上,請在NotePad中打開該文件並保存所需的編碼。 在Linux中,在文本編輯器中也一樣。 希望你的程序運行。

暫無
暫無

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

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