我是python语言的初学者
我试图使用urlopen by 和语句获取文件内容并打印该文件的所有单词。
以下是我所仿效的例子:
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)代码在[医]小提琴网站上运行良好,但在我的本地机器上不工作。
结果应该是这样:
'It','it','the','the',‘time’,'it',‘time’,'it',‘the,’最差‘,'of',’最坏‘,'of',’the,‘the,'age','it','the','age',’the,‘age’,of,‘it’,'the',‘age’,‘it’,‘the’,‘age’,of,‘it’,‘the’,‘age’,‘it’,‘the’,the,‘age’,of,‘it’,‘the’,‘age’,of,the‘the,’age‘,’it‘it,the’the,‘age’,of,the,‘age’,of,the‘the,’age‘,’it‘,’the,the‘the’‘it 'of’it',‘the 'of’it',‘the 'of’Light',‘it’it 'of‘the’it‘it 'it',’the‘it’it‘it’of‘of,’the‘’the 'it',‘it’of‘it’the‘it’‘it’the‘the the,’it‘’it‘it’it,‘the,’the‘the’the,‘the,'the',of’it‘,the’the,'the',of,the,the,the,'of',‘希望’,'it','us','the',‘冬天’,'of',‘绝望’,‘我们’,‘拥有’,‘一切’,‘之前’,‘我们’,‘一无所有’,‘之前’,‘我们’是‘全部’,‘去’,‘直接’,‘'to',’天堂‘,’我们‘,“曾经”、“全部”、“去”、“直接”、“其他”、“方式”、“短”、“短”、“句号”、“是”、“所以”、“远”、“像”、“”、“现在”、“句号”、“那个”、“一些”、“一些”、“它的”、“最吵闹的”、“权威”、“继续”、“继续”,“in”、“of”、“接收”、“for”、“good”、“or”、“for”、“邪恶”、“in”、“the”、“最高级”、“学位”、“of”、“比较”、“only”
我使用的是windows 10和python版本3.6
这是当我试图在本地打印单词时所显示的错误:
line_words = line.decode( 'utf-8‘).split() UnicodeDecodeError:’utf-8‘编解码器无法解码位置1中的字节0x8b :无效开始字节
我还打印了没有解码的单词,以查看它是如何同时出现的(小提琴和本地的):
在中的应用
B‘’it‘b’‘it’b‘’the‘b’b‘b’b‘’of‘b’‘of’b‘b’‘it’b‘b’b‘b’‘the’b‘’it‘b’‘the’b‘b’s b‘’it‘b’b‘’the‘b’‘it’b‘’it‘b’b‘’the b‘b’‘it’b‘’it‘b’b‘’it‘b’b‘’the‘b’‘it’b‘’it‘b’b‘’the‘b’‘it’b‘’the‘b’‘it’b‘b’‘it’b‘B‘b’a‘’b‘’of‘b’‘the’b‘’it‘b’a‘b’‘the’b‘b’a‘b’b‘’it‘b’‘it’b‘’it‘b’b‘’the,b‘’it‘b’‘it,b’b,b‘’it,b‘’it,b‘b,b’‘the,b’‘the,b’b,b‘b,b’‘the,b’‘it,b’b,b‘b,b’‘the,b’‘it,b’b,b‘b,b’‘the,b’‘theB‘’of‘b’‘had’b‘’it‘b’‘us’b‘b’‘the’b‘’of‘b’‘of’b‘b’‘of’b‘’of‘b’‘the’b‘b’‘had’b‘’b‘’the‘b’‘had’b‘b’b‘b’‘had’b‘b’‘the’b‘’the‘’the‘b’b‘’of‘b’‘of b’‘had’b‘b’‘to’b‘’us‘b’‘we’b‘b’‘to’b‘’us‘b’‘we’b‘b’‘us’b‘’us‘b’b‘b’b b‘’all‘b’b‘’to‘’to‘b’‘us’b‘’had‘b’‘us’b‘’to‘b’‘it,b’b‘’us‘b’b,b‘b’b‘b,b’b,b‘b’b,b‘b,b’‘to’‘to’bB‘b’b‘’all‘b’‘on’b‘直接’b‘’the‘b’b‘’on‘b’‘way’b‘’in‘’in‘b’‘short’b‘’the‘b’b‘’the‘b’b‘’the,b‘’in‘b’‘in’b‘’so‘b’‘so’b‘远’b‘b’‘like’b‘b’‘the’,b‘b’‘on’b‘’the,b‘s’b‘’of‘’of‘’of,b‘’its‘,b’b‘吵闹’,b‘b’当局‘,b’b‘坚持,b’‘on’,B‘’in‘b’b‘b’接收‘b’‘for’b‘’good‘b’‘or’b‘’or‘b’b‘’for‘b’b‘’in‘b’‘in’b‘b’‘the’b‘b’最高级‘b’程度‘b’‘of’b‘比较b’b‘’only‘
局部
b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03}\x91Qn\x840',b‘d\xff\xff 7\x14>L?Z\xa9 9\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“
,有人能知道这里发生了什么吗,我怎么解决这个问题呢?
发布于 2018-02-14 13:56:53
密码现在起作用了。
这个问题的发生是因为网络本身,因为改变了网络,所以可能是因为旧网络的一些防火墙配置。
同样,正如蒂埃里·拉苏维尔所建议的,在不改变网络的情况下,我们可以使用请求库,并且它工作得很好。
谢谢。
https://stackoverflow.com/questions/48783263
复制相似问题