繁体   English   中英

UnicodeDecodeError:“ ascii”编解码器无法解码字节0xe4

[英]UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4

我想知道是否有人可以帮助我,我已经尝试过事先搜索,但找不到答案:

我有一个名为info.dat的文件,其中包含:

#
# *** Please be aware that the revision numbers on the control lines may not always
# *** be 1 more than the last file you received. There may have been additional
# *** increments in between.
#
$001,427,2018,04,26
#
# Save this file as info.dat
#

我正在尝试循环文件,获取版本号并将其写入其自己的文件

with open('info.dat', 'r') as file:
    for line in file:
        if line.startswith('$001,'):
            with open('version.txt', 'w') as w:
                version = line[5:8] # Should be 427
                w.write(version + '\n')
                w.close()

尽管这确实写入了正确的信息,但我不断收到以下错误:

Traceback (most recent call last):
File "~/Desktop/backup/test.py", line 4, in <module>
for line in file:
File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 6281: ordinal not in range(128)

尝试添加以下内容时

with open('info.dat', 'r') as file:
    for line in file:
        if line.startswith('$001,'):
            with open('version.txt', 'w') as w:
                version = line[5:8]
                # w.write(version.encode('utf-8') + '\n')
                w.write(version.decode() + '\n')
                w.close()

我收到以下错误

Traceback (most recent call last):
File "~/Desktop/backup/test.py", line 9, in <module>
w.write(version.encode('utf-8') + '\n')
TypeError: can't concat str to bytes

您正在尝试打开一个文本文件,该文件将使用默认编码对每行进行隐式解码,然后使用UTF-8手动对每行进行重新编码,然后将其写入文本文件,这将对UTF-8进行隐式解码再次使用您的默认编码。 那是行不通的。 但好消息是, 正确的做法要简单得多。


如果您知道输入文件为UTF-8(可能不是,请参见下文),则只需以UTF-8而不是默认编码打开文件即可:

with open('info.dat', 'r', encoding='utf-8') as file:
    for line in file:
        if line.startswith('$001,'):
            with open('version.txt', 'w', encoding='utf-8') as w:
                version = line[5:8] # Should be 427
                w.write(version + '\n')
                w.close()

实际上,我非常确定您的文件不是 UTF-8,而是Latin-1(在Latin-1中, \\xa3ä ;在UTF-8中,这是一个3字节序列的开始,该序列可能会编码一个CJK字符)。 如果是这样,您可以使用正确的编码而不是错误的编码执行相同的操作,现在它可以工作。


但是,如果您不知道编码是什么,请不要猜测。 只是坚持使用二进制模式。 这意味着传递rbwb模式而不是rw ,并使用bytes常量:

with open('info.dat', 'rb') as file:
    for line in file:
        if line.startswith(b'$001,'):
            with open('version.txt', 'wb') as w:
                version = line[5:8] # Should be 427
                w.write(version + b'\n')
                w.close()

无论哪种方式,都无需在任何地方调用encodedecode 只需让文件对象为您处理,并在任何地方都只处理一种类型(无论是str还是bytes )。

encode()返回字节,但“ \\ n”是字符串,您需要将字符串形式的字节转换为字节+字节,因此请尝试此操作

w.write(version.encode('utf-8') + b'\n')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM