简体   繁体   English

通过Python读取txt文件时出现编解码器问题

[英]Codec issue while reading txt file via Python

Having an issue while trying to open .txt file (which contains only pure text in it) with Python 3.6 using simple open () approach: 在尝试使用简单的open ()方法使用Python 3.6打开.txt文件(其中仅包含纯文本open ()遇到问题:

with open('3003.txt', 'r') as myfile:
    data=myfile.read()

It gives the errortrace like that: 它给出了这样的错误跟踪:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe0 in position 182: invalid continuation byte

I used couple of googled approaches but they dont solve the deal. 我使用了几种谷歌搜索方法,但是它们不能解决交易。 What should be done to solve this? 应该怎么做才能解决这个问题?

You can use Python 3 open() style file handler which streams bytestrings: 您可以使用Python 3 open()样式文件处理程序来流字节字符串:

open('3003.txt', 'rb') as myfile:
    data=myfile.read()

Note the 'b' meaning binary mode 注意“ b”表示二进制模式

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

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