python读取文件乱码
方法一:使用codecs
import codecs
f = codecs.open('nlpir/Readme.txt','r','GBK')
line = f.readline()
while line:
print line,
line = f.readline()
f.close()
上面的方法很慢,可以直接读取整个文件
codecs.open('nlpir/Readme.txt','r','GBK').read()还有这样读的readlines()方法二: #读取文档 def read_file(): path1='K:\\SogouC.reduced\\Reduced\\C000008\\10.txt' f = open(path1,'r+') file_list = f.read().decode("gbk") print file_list f.close() #逐行读取文档 def read_file_line(): path1='K:\\SogouC.reduced\\Reduced\\C000008\\10.txt' f = open(path1,'r+') line = f.readline() while line: print line.decode('gbk') line = f.readline() f.close()
python读取文件乱码的更多相关文章
- 解决 python 读取文件乱码问题(UnicodeDecodeError)
解决 python 读取文件乱码问题(UnicodeDecodeError) 确定你的文件的编码,下面的代码将以'utf-8'为例,否则会忽略编码错误导致输出乱码 解决方案一 with open(r' ...
- 【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: 'gbk' codec can't decode byte 0x80 in position 205: illegal multibyte sequence
python读取文件时提示"UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal m ...
- python 读取文件read.csv报错 OSError: Initializing from file failed
小编在用python 读取文件read.csv的时候 报了一个错误 OSError: Initializing from file failed 初始化 文件失败 检查了文件路径,没问题 那应该是我文 ...
- 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 读取文件中unicode编码转成中文显示问题
Python读取文件中的字符串已经是unicode编码,如:\u53eb\u6211,需要转换成中文时有两种方式 1.使用eval: eval("u"+"\'" ...
- python读取文件首行和最后一行
python读取文件最后一行两种方式 1)常规方法:从前往后依次读取 步骤:open打开文件. 读取文件,把文件所有行读入内存. 遍历所有行,提取指定行的数据. 优点:简单,方便 缺点:当文件大了以后 ...
- python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件
python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 python操作txt文件中 ...
- python 读取文件时报错: UnicodeDecodeError: 'gbk' codec can't decode byte 0xa4 in position 127: illegal multibyte sequence
UnicodeDecodeError: 'gbk' codec can't decode byte 0xa4 in position 127: illegal multibyte sequence p ...
随机推荐
- 字符串的 Base64 加密和解密
base64加密: FORM encode_base64 USING p_business “字符串string CHANGING p_base64. DATA l_xstring TYPE xstr ...
- id - 显示真实和有效的 UID 和 GID
总览 (SYNOPSIS) id [OPTION]... [USERNAME] 描述 (DESCRIPTION) 显示 USERNAME 或者 当前 用户 的 信息. -a 忽略, 同 其它 版本 兼 ...
- linux 桥接模式下 固定ip 设置
DEVICE=eht0 #网卡名称BOOTPROTO=none #关闭自动获取 dhcp IPADDR=192.168.0.178 #ip地址GATEWAY=192.168.0.1 DN ...
- C#操作目录和文件
C#操作目录和文件 创建目录和文件 1.通过Path类的Combine方法可以合并路径. string activeDir = @"C:\myDir"; string newPa ...
- Saving Tang Monk II HihoCoder - 1828 2018北京赛站网络赛A题
<Journey to the West>(also <Monkey>) is one of the Four Great Classical Novels of Chines ...
- 【JAVA】Maven profiles控制多环境数据源日志打包(转载)
https://blog.csdn.net/qq_17213067/article/details/81661295
- Laravel5.5去除URL中的index.php生成优雅链接
在使用Apache情况下: Laravel 框架通过 public/.htaccess 文件来让网址中不需要 index.php.如果你的服务器是使用 Apache ,请确认是否有开启 mod_rew ...
- java课堂测试2(两种方式)
实验源代码 这是不使用数组形式的源代码 /* 2017/10/10 王翌淞 课堂测试2 */import java.util.Scanner; public class Number { public ...
- Olympic Game
每次奥运会期间,大家都非常关注奖牌榜排名的情况. 现在我们假设奖牌榜的排名规则,按优先级从高到低如下: 金牌 数量多的排在前面: 银牌 数量多的排在前面: 铜牌 数量多的排在前面: 若以上三个条件仍无 ...
- Mybatis-Plus和Mybatis的区别
1.List item 区别一如果Mybatis Plus是扳手,那Mybatis Generator就是生产扳手的工厂.通俗来讲——MyBatis:一种操作数据库的框架,提供一种Mapper类,支持 ...