【问题标题】:UnicodeDecodeError: 'ascii' codec can't decode byte 0x92? [duplicate]UnicodeDecodeError:“ascii”编解码器无法解码字节 0x92? [复制]
【发布时间】:2016-05-07 01:51:14
【问题描述】:

所以我试图从 .txt 文件中读取数据,然后找到最常见的 30 个单词并将它们打印出来。但是,每当我阅读我的 txt 文件时,我都会收到错误消息:

"UnicodeDecodeError: 'ascii' codec can't decode byte 0x92 in position 338: ordinal not in range(128)".

这是我的代码:

filename = 'wh_2015_national_security_strategy_obama.txt'
#catches the year of named in the file
year = filename[0:4]
ecount = 30
#opens the file and reads it
file = open(filename,'r').read()   #THIS IS WHERE THE ERROR IS
#counts the characters, then counts the lines, replaces the non word characters, slipts the list and changes it all to lower case.
numchar = len(file)
numlines = file.count('\n')
file = file.replace(",","").replace("'s","").replace("-","").replace(")","")
words = file.lower().split()
dictionary = {}
#this is a dictionary of all the words to not count for the most commonly used. 
dontcount = {"the", "of", "in", "to", "a", "and", "that", "we", "our", "is", "for", "at", "on", "as", "by", "be", "are", "will","this", "with", "or",
             "an", "-", "not", "than", "you", "your", "but","it","a","and", "i", "if","they","these","has","been","about","its","his","no"
             "because","when","would","was", "have", "their","all","should","from","most", "were","such","he", "very","which","may","because","--------"
             "had", "only", "no", "one", "--------", "any", "had", "other", "those", "us", "while",
             "..........", "*", "$", "so", "now","what", "who", "my","can", "who","do","could", "over", "-",
             "...............","................", "during","make","************",
             "......................................................................", "get", "how", "after",
             "..................................................", "...........................", "much", "some",
             "through","though","therefore","since","many", "then", "there", "–", "both", "them", "well", "me", "even", "also", "however"}
for w in words:
    if not w in dontcount:
        if w in dictionary:
            dictionary[w] +=1
        else:
            dictionary[w] = 1
num_words = sum(dictionary[w] for w in dictionary)
#This sorts the dictionary and makes it so that the most popular is at the top.
x = [(dictionary[w],w) for w in dictionary]
x.sort()
x.reverse()
#This prints out the number of characters, line, and words(not including stop words.
print(str(filename))
print('The file has ',numchar,' number of characters.')
print('The file has ',numlines,' number of lines.')
print('The file has ',num_words,' number of words.')
#This provides the stucture for how the most common words should be printed out
i = 1
for count, word in x[:ecount]:
    print("{0}, {1}, {2}".format(i,count,word))
    i+=1

【问题讨论】:

  • 查看我链接到的帖子和Python 3 docs for open,尤其是它的encoding 参数。对于 Python 2,open 的“新”版本位于 io.open 中。 PS:该字节很可能是非标准(Microsoft)右单引号,经常被误用作“卷曲”撇号。
  • 以上都不是 - 所有这些问题和答案都涉及 Python 2。没有人会帮助 OP 解决与 Python 3 的 TextIOWrapper 引发异常相关的非常简单的问题,必须通过选择正确的编码来纠正

标签: python character-encoding python-unicode


【解决方案1】:

在 Python 3 中,当以文本模式(默认)打开文件时,Python 会使用您的环境设置来选择合适的编码。

如果无法解析(或者您的环境专门定义了 ASCII),那么它将使用 ASCII。这就是你的情况。

如果 ASCII 解码器发现任何不是 ASCII 的东西,那么它会抛出一个错误。在您的情况下,它在字节 0x92 上引发错误。这不是有效的 ASCII,也不是有效的 UTF-8。在windows-1252 编码中确实有意义,但是,它是(智能引号/“右单引号”)。它在其他 8 位代码页中也可能有意义,但您必须自己了解或解决。

要让您的代码读取windows-1252 编码文件,您需要将open() 命令更改为:

file = open(filename, 'r', encoding='windows-1252').read()

【讨论】:

    【解决方案2】:

    我正在学习 python,所以请记住这一点。

    file = open(filename,'r').read() #这是错误所在

    根据我目前所学到的知识,您的阅读与 open() 对象的创建相结合。 open() 函数创建文件句柄,read() 函数将文件读入字符串。我认为这两个函数都会返回成功/失败,或者在 open() 函数的情况下部分返回文件对象引用。我不确定它们是否可以成功组合。

    到目前为止,我所学到的知识是分两步完成的。 即

    file = open(filename, 'r') # 创建对象 myString = file.read() # 将整个对象读入字符串

    open() 函数创建文件对象,因此可能返回对象编号,或成功/失败。

    对象上使用了 read、read(n)、readline() 或 readlines() 函数。

    .read 将整个文件读入单个字符串 .read(n) 将接下来的 n 个字节读入字符串 .readline() 将下一行读入字符串 .readline() 将整个文件读入字符串列表

    您可以将它们分开,看看是否会发生相同的结果???只是一个新手的想法:)

    【讨论】:

    • 在读取之前将类似文件的对象分配给局部变量不会更改该文件的内容,也不会更改它们从字节转换为字符串的方式,这就是导致UnicodeDecodeError 的原因。查看openencodingerrors 参数,以及它返回的TextIOBase(“文本文件”)的各种read 相关方法。
    猜你喜欢
    • 1970-01-01
    • 2013-08-20
    • 2014-04-09
    • 2018-08-02
    • 2013-09-23
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多