Python读取本地文件时出现错误UnicodeDecodeError: 'gbk' codec can't decode byte 0x94 in position 5702: illegal multibyte sequence

解决方法:

def count_words(filename):
try:
with open(filename) as f_obj:
contents = f_obj.read()
except FileNotFoundError:
msg = "Sorry, the file " + filename + " does not exist."
print(msg)
else:
words = contents.split()
num_words = len(words)
print("The file " + "filename " + " has about " + str(num_words) + " words.") filenames = ['petrel.txt', 'TheBlackCat.txt', 'Grist.txt']
for filename in filenames:
count_words(filename)

定位到第4行,添加代码,encoding='utf-8'

 with open(filename, "r", encoding='utf-8') as f_obj:

Python读取本地文件时出现错误UnicodeDecodeError的更多相关文章

  1. python 读取本地文件批量插入mysql

    Uin_phone.txt 本地文件内容 有1000条,这里只是展示前几条,供参考 133584752 133584759 133584764 133584773 133584775 13358477 ...

  2. python读取ini文件时,特殊字符的读取

    前言: 使用python在读取配置文件时,由于配置文件中存在特殊字符,读取时出现了以下错误: configparser.InterpolationSyntaxError: '%' must be fo ...

  3. 解决Requests中文乱码【有用】,读取htm文件 读取txt文件报错:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0

    打开这个网址https://blog.csdn.net/chaowanghn/article/details/54889835 python在open读取txt文件时,出现UnicodeDecodeE ...

  4. Python读取CSV文件,报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xa7 in position 727: illegal multibyte sequence

    Python读取CSV文件,报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xa7 in position 727: illegal mul ...

  5. FileReader读取本地文件

    FileReader是一种异步读取文件机制,结合input:file可以很方便的读取本地文件. 一.input:type[file] file类型的input会渲染为一个按钮和一段文字.点击按钮可打开 ...

  6. Python读取大文件的"坑“与内存占用检测

    python读写文件的api都很简单,一不留神就容易踩"坑".笔者记录一次踩坑历程,并且给了一些总结,希望到大家在使用python的过程之中,能够避免一些可能产生隐患的代码. 1. ...

  7. PHP 与Python 读取大文件的区别

    php读取大文件的方法   <?php function readFile($file) { # 打开文件 $handle = fopen($file, 'rb'); while (feof($ ...

  8. python读取中文文件编码问题

    python 读取中文文件后,作为参数使用,经常会遇到乱码或者报错asii错误等. 我们需要对中文进行decode('gbk') 如我有一个data.txt文件有如下内容: 百度 谷歌 现在想读取文件 ...

  9. 【转】flash air中读取本地文件的三种方法

    actionscript中读取本地文件操作有两种代码如下 1.使用File和FileStream两个类,FileStream负责读取数据的所以操作:(同步操作) var stream:FileStre ...

  10. H5读取本地文件操作

    H5读取本地文件操作 本文转自:转:http://hushicai.com/2014/03/29/html5-du-qu-ben-di-wen-jian.html感谢大神分享. 常见的语言比如php. ...

随机推荐

  1. pysimplegui之系统托盘图标创建

    在 PySimpleGUI(tkinter 版本)上运行时,系统托盘图标为 PNG 和 GIF 格式.PNG.GIF 和 ICO 格式适用于 Wx 和 Qt 端口. 指定"图标"时 ...

  2. [Git]Git统计代码行数

    1 前言 今天,有这么一个需求:小组老大要求咱们[每个人]把[上个月]的[代码行数]统计一下并上报. 成,统计就统计,但那么多项目,总不能让我用手去数吧?何况,时间久了,自己也不清楚自己改了哪些地方了 ...

  3. Java设计模式 —— 建造者模式

    8 建造者模式 8.1 建造者模式概述 Builder Pattern:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 建造者模式可以将部件本身和它们的组装过程分开,关注如 ...

  4. Sphinx 配置文件说明

    一.    创建sphinx.conf文件 其结构组成主要如下: Source 源名称1{                //指定数据源 一些配置 } Index 索引名称1{ Source=源名称1 ...

  5. docker上面部署nginx-waf 防火墙“modsecurity”,使用CRS规则,搭建WEB应用防火墙

    web防火墙(waf)免费开源的比较少,并且真正可以商用的WAF少之又少,modsecurity 是开源防火墙鼻祖并且有正规公司在维护着,目前是https://www.trustwave.com在维护 ...

  6. Prism Sample 12-UsingCompositeCommands

    本例中,主页是一个按钮,绑定了一个复合命令,然后下面一个TabControl <Grid> <Grid.RowDefinitions> <RowDefinition He ...

  7. ai问答:使用 Vue3 组合式API 和 TS 封装 echarts 折线图

    使用这个组件时,只需要传入合适的chartData数组,就可以渲染一个折线图,并且响应数据变化. <template> <div ref="chart" styl ...

  8. WARNING: The repository located at mirrors.aliyun.com is not a trusted or secure host and is being

    更换下载源: https://pypi.tuna.tsinghua.edu.cn/simple/

  9. npm install报错node-sass@7.0.1 postinstall: `node scripts/build.js`

    在控制台执行 即可 npm config set sass_binary_site=https://npm.taobao.org/mirrors/node-sass

  10. phpstudy-sqlilabs-less-3

    题目:GET - Error based - Single quotes with twist 基于错误的单引号GET型变形注入 ?id=1 )and 1=2--+ ?id=1 "and 1 ...