【发布时间】:2011-11-01 09:43:07
【问题描述】:
我正在尝试从以下代码中的地址读取一些 utf-8 文件。它适用于大多数文件,但对于某些文件,urllib2(和 urllib)无法读取。
这里的明显答案是第二个文件已损坏,但奇怪的是 IE 读取它们都没有问题。该代码已在 XP 和 Linux 上进行了测试,结果相同。有什么建议吗?
import urllib2
#This works:
f=urllib2.urlopen("http://www.gutenberg.org/cache/epub/145/pg145.txt")
line=f.readline()
print "this works: %s)" %(line)
line=unicode(line,'utf-8') #... works fine
#This doesn't
f=urllib2.urlopen("http://www.gutenberg.org/cache/epub/144/pg144.txt")
line=f.readline()
print "this doesn't: %s)" %(line)
line=unicode(line,'utf-8')#...causes an exception:
【问题讨论】: