Python:如何读取和解析utf-8 Unicode文本文件? [英] Python: How do I read and parse a unicode utf-8 text file?

查看:511
本文介绍了Python:如何读取和解析utf-8 Unicode文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Excel导出UTF-8文本,我想使用Python读取和解析传入的数据.我已经阅读了所有在线信息,所以我已经尝试过了,例如:

I am exporting UTF-8 text from Excel and I want to read and parse the incoming data using Python. I've read all the online info so I've already tried this, for example:

 txtFile = codecs.open( 'halout.txt', 'r', 'utf-8' )
 for line in txtFile:
  print repr( line )

我得到的错误是:

UnicodeDecodeError:'utf8'编解码器无法解码位置0:意外的代码字节中的字节0xff

UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: unexpected code byte

在十六进制编辑器中查看文本文件,第一个值是我也尝试过的FFFE:

Looking at the text file in a Hex editor, the first values are FFFE I've also tried:

txtFile.seek( 2 )

紧接在打开"之后,但这只会导致其他错误.

right after the 'open' but that just causes a different error.

推荐答案

这是 BOM

编辑,从评论中看,这似乎是utf-16 bom

EDIT, from the coments, it seems to be a utf-16 bom

codecs.open('foo.txt', 'r', 'utf-16')

应该工作.

这篇关于Python:如何读取和解析utf-8 Unicode文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆