Python 3.6 openurl 行为在 windows OS 中有所不同
Python 3.6 openurl behaviour is different in windows OS
我是 python 语言的初学者
我正在尝试通过 with 语句使用 urlopen 来获取文件内容并打印此文件的所有单词。
这是我遵循的示例:
from urllib.request import urlopen
with urlopen('http://sixty-north.com/c/t.txt') as story:
story_words = []
for line in story:
line_words = line.decode('utf-8').split()
for word in line_words:
story_words.append(word)
print(story_words)
该代码在 pyfiddle 网站上运行良好,但在我的本地计算机上无法运行。
结果应该是这样的:
['It', 'was', 'the', 'best', 'of', 'times', 'it', 'was', 'the', 'worst', 'of', 'times', 'it', 'was', 'the', 'age', 'of', 'wisdom', 'it', 'was', 'the', 'age', 'of', 'foolishness', 'it', 'was', 'the', 'epoch', 'of', 'belief', 'it', 'was', 'the', 'epoch', 'of', 'incredulity', 'it', 'was', 'the', 'season', 'of', 'Light', 'it', 'was', 'the', 'season', 'of', 'Darkness', 'it', 'was', 'the', 'spring', 'of', 'hope', 'it', 'was', 'the', 'winter', 'of', 'despair', 'we', 'had', 'everything', 'before', 'us', 'we', 'had', 'nothing', 'before', 'us', 'we', 'were', 'all', 'going', 'direct', 'to', 'Heaven', 'we', 'were', 'all', 'going', 'direct', 'the', 'other', 'way', 'in', 'short', 'the', 'period', 'was', 'so', 'far', 'like', 'the', 'present', 'period', 'that', 'some', 'of', 'its', 'noisiest', 'authorities', 'insisted', 'on', 'its', 'being', 'received', 'for', 'good', 'or', 'for', 'evil', 'in', 'the', 'superlative', 'degree', 'of', 'comparison', 'only']
我正在使用 windows 10 和 python 版本 3.6
这是我尝试在本地打印单词时向我显示的错误:
Traceback (most recent call last):
File "", line 4, in
line_words = line.decode('utf-8').split()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
我还打印了没有 解码的单词 看看它是如何在两者(pyfiddle 和本地)中出现的:
在 pyfiddle 中:
[b'It', b'was', b'the', b'best', b'of', b'times', b'it', b'was', b'the', b'worst', b'of', b'times', b'it', b'was', b'the', b'age', b'of', b'wisdom', b'it', b'was', b'the', b'age', b'of', b'foolishness', b'it', b'was', b'the', b'epoch', b'of', b'belief', b'it', b'was', b'the', b'epoch', b'of', b'incredulity', b'it', b'was', b'the', b'season', b'of', b'Light', b'it', b'was', b'the', b'season', b'of', b'Darkness', b'it', b'was', b'the', b'spring', b'of', b'hope', b'it', b'was', b'the', b'winter', b'of', b'despair', b'we', b'had', b'everything', b'before', b'us', b'we', b'had', b'nothing', b'before', b'us', b'we', b'were', b'all', b'going', b'direct', b'to', b'Heaven', b'we', b'were', b'all', b'going', b'direct', b'the', b'other', b'way', b'in', b'short', b'the', b'period', b'was', b'so', b'far', b'like', b'the', b'present', b'period', b'that', b'some', b'of', b'its', b'noisiest', b'authorities', b'insisted', b'on', b'its', b'being', b'received', b'for', b'good', b'or', b'for', b'evil', b'in', b'the', b'superlative', b'degree', b'of', b'comparison', b'only']
本地:
[b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03}\x91Qn\x840', b'D\xff\xf7\x14>L?Z\xa9\x97\x08d', b"\xd6\x86\x18\xd9\x06\xb4\xb7\xaf\xb3\xabU\xbb\x15\xed\x17\xc1o\xec\x8c'\x1fNG2\xf2\x02\x1aN2\x91\xf3\x02\xbb\xf078D\xff", b'iF\xaf\x1flY\x96\x130\x89T\xb6\xd2
/mXe,\x9d\x0f\xa8\x8c\xe9\x14q\x1b\x15y\xab\xec\xb7\x9f\xdc\x90LZ\x17|\xf2\xfc\x1c\xbd%\xbd\xfe\xbe\xd3V\xe56wZd\xc5\xcbz\xdc\x1c\xdaI\x86\xad\x89\xf5r\x80J\xca\x84\x1dz\xf3\xd2\xdb\x06L\xa2\xa0\xcd\x9e\xac\xc9', b'8\x10\xc7T+\xcd\xd2Yf\xc5\xe8\xe4B\xefH;\xda?\x92\xb0\x11\x03\xc3\xc5\x91b\xddFVD\x1f\xe5\x15\xca\x92\xeffMhJJ\x95\xafx', b"\x85\xa1\xf9S\xe2%yh\x96\x9e|\xecg\xe1\x91\x8d\xfb\xa3\xa6\xcdc\x1e{\xfcD\xae\xc6\xe6\xc8\x14Qu\xd1\x80\xee#\x80\xf7\xa8\xc66a-\xa6\xc57\xce\x17\xec\xa3\xe7\x11\xe1\x167\xd5\xe4!\x8c\xa8f\xc5\xfd\x8dGY\xd6\xa4|\x8f\xbe\xd5\xdb\x17\xc5v'\x1dQ\x02\x00\x00"]
谁能告诉我这里发生了什么,我该如何解决?
代码现在可以运行了。
这个问题是因为网络本身的原因,因为换了网络之后还可以正常使用,所以可能是旧网络的一些防火墙配置导致的。
也作为 Thierry Lathuille suggest, without changing the network we can use the Requests 库,它工作得很好。
谢谢。
我是 python 语言的初学者
我正在尝试通过 with 语句使用 urlopen 来获取文件内容并打印此文件的所有单词。
这是我遵循的示例:
from urllib.request import urlopen
with urlopen('http://sixty-north.com/c/t.txt') as story:
story_words = []
for line in story:
line_words = line.decode('utf-8').split()
for word in line_words:
story_words.append(word)
print(story_words)
该代码在 pyfiddle 网站上运行良好,但在我的本地计算机上无法运行。
结果应该是这样的:
['It', 'was', 'the', 'best', 'of', 'times', 'it', 'was', 'the', 'worst', 'of', 'times', 'it', 'was', 'the', 'age', 'of', 'wisdom', 'it', 'was', 'the', 'age', 'of', 'foolishness', 'it', 'was', 'the', 'epoch', 'of', 'belief', 'it', 'was', 'the', 'epoch', 'of', 'incredulity', 'it', 'was', 'the', 'season', 'of', 'Light', 'it', 'was', 'the', 'season', 'of', 'Darkness', 'it', 'was', 'the', 'spring', 'of', 'hope', 'it', 'was', 'the', 'winter', 'of', 'despair', 'we', 'had', 'everything', 'before', 'us', 'we', 'had', 'nothing', 'before', 'us', 'we', 'were', 'all', 'going', 'direct', 'to', 'Heaven', 'we', 'were', 'all', 'going', 'direct', 'the', 'other', 'way', 'in', 'short', 'the', 'period', 'was', 'so', 'far', 'like', 'the', 'present', 'period', 'that', 'some', 'of', 'its', 'noisiest', 'authorities', 'insisted', 'on', 'its', 'being', 'received', 'for', 'good', 'or', 'for', 'evil', 'in', 'the', 'superlative', 'degree', 'of', 'comparison', 'only']
我正在使用 windows 10 和 python 版本 3.6
这是我尝试在本地打印单词时向我显示的错误:
Traceback (most recent call last): File "", line 4, in line_words = line.decode('utf-8').split() UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
我还打印了没有 解码的单词 看看它是如何在两者(pyfiddle 和本地)中出现的:
在 pyfiddle 中:
[b'It', b'was', b'the', b'best', b'of', b'times', b'it', b'was', b'the', b'worst', b'of', b'times', b'it', b'was', b'the', b'age', b'of', b'wisdom', b'it', b'was', b'the', b'age', b'of', b'foolishness', b'it', b'was', b'the', b'epoch', b'of', b'belief', b'it', b'was', b'the', b'epoch', b'of', b'incredulity', b'it', b'was', b'the', b'season', b'of', b'Light', b'it', b'was', b'the', b'season', b'of', b'Darkness', b'it', b'was', b'the', b'spring', b'of', b'hope', b'it', b'was', b'the', b'winter', b'of', b'despair', b'we', b'had', b'everything', b'before', b'us', b'we', b'had', b'nothing', b'before', b'us', b'we', b'were', b'all', b'going', b'direct', b'to', b'Heaven', b'we', b'were', b'all', b'going', b'direct', b'the', b'other', b'way', b'in', b'short', b'the', b'period', b'was', b'so', b'far', b'like', b'the', b'present', b'period', b'that', b'some', b'of', b'its', b'noisiest', b'authorities', b'insisted', b'on', b'its', b'being', b'received', b'for', b'good', b'or', b'for', b'evil', b'in', b'the', b'superlative', b'degree', b'of', b'comparison', b'only']
本地:
[b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03}\x91Qn\x840', b'D\xff\xf7\x14>L?Z\xa9\x97\x08d', b"\xd6\x86\x18\xd9\x06\xb4\xb7\xaf\xb3\xabU\xbb\x15\xed\x17\xc1o\xec\x8c'\x1fNG2\xf2\x02\x1a
N2\x91\xf3\x02\xbb\xf078D\xff", b'iF\xaf\x1flY\x96\x130\x89T\xb6\xd2
/mXe,\x9d\x0f\xa8\x8c\xe9\x14q\x1b\x15y\xab\xec\xb7\x9f\xdc\x90LZ\x17|\xf2\xfc\x1c\xbd%\xbd\xfe\xbe\xd3V\xe56wZd\xc5\xcbz\xdc\x1c\xdaI\x86\xad\x89\xf5r\x80J\xca\x84\x1dz\xf3\xd2\xdb\x06L\xa2\xa0\xcd\x9e\xac\xc9', b'8\x10\xc7T+\xcd\xd2Yf\xc5\xe8\xe4B\xefH;\xda?\x92\xb0\x11\x03\xc3\xc5\x91b\xddFVD\x1f\xe5\x15\xca\x92\xeffMhJJ\x95\xafx', b"\x85\xa1\xf9S\xe2%yh\x96\x9e|\xecg\xe1\x91\x8d\xfb\xa3\xa6\xcdc\x1e{\xfcD\xae\xc6\xe6\xc8\x14Qu\xd1\x80\xee#\x80\xf7\xa8\xc66a-\xa6\xc57\xce\x17\xec\xa3\xe7\x11\xe1\x167\xd5\xe4!\x8c\xa8f\xc5\xfd\x8dGY\xd6\xa4|\x8f\xbe\xd5\xdb\x17\xc5v'\x1dQ\x02\x00\x00"]
谁能告诉我这里发生了什么,我该如何解决?
代码现在可以运行了。
这个问题是因为网络本身的原因,因为换了网络之后还可以正常使用,所以可能是旧网络的一些防火墙配置导致的。
也作为 Thierry Lathuille suggest, without changing the network we can use the Requests 库,它工作得很好。
谢谢。