Python读取文件编码及内容
Python读取文件编码及内容
最近做一个项目,需要读取文件内容,但是文件的编码方式有可能都不一样。有的使用GBK,有的使用UTF8。所以在不正确读取的时候会出现如下错误:
UnicodeDecodeError: 'gbk' codec can't decode byte
而且当你使用rb模式读取文件时候,返回的结果通过django返回的json会出现下面错误:
TypeError: b'\xbc\x8c\xe6\x9c\xaa\xe6\x9d\xa5' is not JSON serializable
总之就是编码不对,所以要先能识别文件的编码方式,然后根据此编码方式进行对文件编码,最后返回文件内容。
解决方法如下:
with open("your_file", 'rb') as fp:
file_data = fp.read()
result = chardet.detect(file_data)
file_content = file_data.decode(encoding=result['encoding'])
注: chardet是第一个第三方库,你需要自己使用pip进行安装。
@完
Python读取文件编码及内容的更多相关文章
- python读取文件指定行内容
python读取文件指定行内容 import linecache text=linecache.getline(r'C:\Users\Administrator\Desktop\SourceCodeo ...
- Python 读取文件下所有内容、获取文件名、截取字符、写回文件
# coding=gbk import os import os.path #读取目录下的所有文件,包括嵌套的文件夹 def GetFileList(dir, fileList): newDir ...
- Python读取文件编码解码问题
用chardet检测编码 import chardet raw = open("model.json", 'rb').read() result = chardet.detect( ...
- python读取文件编码转换问题
encode(编码) decode(解码) encoding(编码格式) #-*- coding:utf-8 -*- import chardet #用于查看编码 with open(&quo ...
- Python读取文件内容与存储
Python读取与存储文件内容 一..csv文件 读取: import pandas as pd souce_data = pd.read_csv(File_Path) 其中File_path是文件的 ...
- Python 读取文件中unicode编码转成中文显示问题
Python读取文件中的字符串已经是unicode编码,如:\u53eb\u6211,需要转换成中文时有两种方式 1.使用eval: eval("u"+"\'" ...
- python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib
python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib ...
- 【python】python读取文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 2: illegal multibyte sequence
python读取文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 2: illegal multibyte ...
- 解决 python 读取文件乱码问题(UnicodeDecodeError)
解决 python 读取文件乱码问题(UnicodeDecodeError) 确定你的文件的编码,下面的代码将以'utf-8'为例,否则会忽略编码错误导致输出乱码 解决方案一 with open(r' ...
随机推荐
- centos 7 Hadoop2.7.4完全分布式搭建(一)
(一)系统准备与安装 1.准备下载centos7 (百度自行下载)可以到开源镜像站下载,速度比较快,比如清华的或者阿里的 在vmware上安装 这里我用的是vmware12 打开Vmware 选择文件 ...
- [LeetCode 题解]: Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- Android之常用开发框架
1.Rajawali介绍:安卓的OpenGL ES 2.0/3.0 引擎.可以用于制作普通应用或者动态壁纸,当然也可以用于制作游戏.项目地址: https://github.com/Rajawali/ ...
- scvmm2008 错误 2912 0x80041001
执行scvmm系列作业时抛出错误 2912 0x80041001. 这个原因是由于主机和vmm通信媒介bits服务挂起所引起的,bits全称Background Intelligent Transfe ...
- django view function
view function 的几种返回值 return HttpResponse(html) return HttpResponseNotFound(html) raise Http404(" ...
- 【QTP专题】02_时间同步点问题
一.什么是同步点 同步点是指在一个测试过程中,指示QuickTest等待应用程序中某个特定过程运行完成以后再运行下一步操作.Waits until the specified object prope ...
- fatal: unable to auto-detect email address (got 'tim@newton.(none)')的解决方法
问题描述: 使用git commit -m "wrote a readme file",就遇到了这个问题** Please tell me who you are. Run git ...
- OCP2018最新题库,052新题库及答案整理-25题
25.Which is true about logical and physical database structures? (Choose the best answer) A. An undo ...
- “全栈2019”Java第四十二章:静态代码块与初始化顺序
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- php中使用PHPExcel读写excel(xls)文件的方法
首先从GitHub上下载 excel的相关类库 下载地址:https://github.com/PHPOffice/PHPExcel 以下是从excel中获取数据 <?php /** * * @ ...