【发布时间】:2011-05-07 10:55:04
【问题描述】:
Python 3.1.2(r312:79147,2010 年 11 月 9 日,09:41:54)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] 在 linux2 上
输入“帮助”、“版权”、“信用”或“许可”以获取更多信息。
>>> open("/home/madsc13ntist/test_file.txt", "r").readlines()[6]
回溯(最近一次通话最后):
文件“”,第 1 行,在
解码中的文件“/usr/local/lib/python3.1/codecs.py”,第 300 行
(结果,消耗)= self._buffer_decode(数据,self.errors,最终)
UnicodeDecodeError:“utf8”编解码器无法解码位置 2230 中的字节 0xae:意外的代码字节
然而……
Python 2.4.3(#1,2010 年 9 月 8 日,11:37:47) [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] 在 linux2 上 输入“帮助”、“版权”、“信用”或“许可”以获取更多信息。 >>> open("/home/madsc13ntist/test_file.txt", "r").readlines()[6] '2010-06-14 21:14:43 613 xxx.xxx.xxx.xxx 200 TCP_NC_MISS 4198 635 GET http www.thelegendssportscomplex.com 80 /thumbnails/t/sponsors/145x138/007.gif - - - 直接 www.thelegendssportscomplex .com 图片/gif http://www.thelegendssportscomplex.com/ “Mozilla/4.0(兼容;MSIE 8.0;Windows NT 5.1;Trident/4.0;.NET CLR 2.0.50727;InfoPath.1;MS-RTC LM 8) " 观察到 "运动/娱乐" - xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx\r\n'有谁知道为什么 .readlines()[6] 对 python-3 不起作用但在 2.4 中起作用?
还有...我认为 0xAE 是 ®
【问题讨论】:
-
这是一个编码错误。索引与它无关(它甚至不被评估)。
-
在 Python3 中使用
open(..,'rb')来模拟 Python 2.x 的行为。
标签: python-3.x readlines