原始代码:
import keras import numpy as np path = keras.utils.get_file( 'nietzsche.txt', origin='https://s3.amazonaws.com/text-datasets/nietzsche.txt' ) text = open(path).read().lower() print('Corpus length:', len(text))
报错情况:
D:\Anaconda\envs\tensorflow\python.exe D:/PYCHARMprojects/Dailypractise/p23.py Downloading data from https://s3.amazonaws.com/text-datasets/nietzsche.txt Traceback (most recent call last): File "D:\Anaconda\envs\tensorflow\lib\urllib\request.py", line 1354, in do_open h.request(req.get_method(), req.selector, req.data, headers, File "D:\Anaconda\envs\tensorflow\lib\http\client.py", line 1255, in request self._send_request(method, url, body, headers, encode_chunked) File "D:\Anaconda\envs\tensorflow\lib\http\client.py", line 1301, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "D:\Anaconda\envs\tensorflow\lib\http\client.py", line 1250, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "D:\Anaconda\envs\tensorflow\lib\http\client.py", line 1010, in _send_output self.send(msg) File "D:\Anaconda\envs\tensorflow\lib\http\client.py", line 950, in send self.connect() File "D:\Anaconda\envs\tensorflow\lib\http\client.py", line 1424, in connect self.sock = self._context.wrap_socket(self.sock, File "D:\Anaconda\envs\tensorflow\lib\ssl.py", line 500, in wrap_socket return self.sslsocket_class._create( File "D:\Anaconda\envs\tensorflow\lib\ssl.py", line 1040, in _create self.do_handshake() File "D:\Anaconda\envs\tensorflow\lib\ssl.py", line 1309, in do_handshake self._sslobj.do_handshake() ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\keras\utils\data_utils.py", line 278, in get_file urlretrieve(origin, fpath, dl_progress) File "D:\Anaconda\envs\tensorflow\lib\urllib\request.py", line 247, in urlretrieve with contextlib.closing(urlopen(url, data)) as fp: File "D:\Anaconda\envs\tensorflow\lib\urllib\request.py", line 222, in urlopen return opener.open(url, data, timeout) File "D:\Anaconda\envs\tensorflow\lib\urllib\request.py", line 525, in open response = self._open(req, data) File "D:\Anaconda\envs\tensorflow\lib\urllib\request.py", line 542, in _open result = self._call_chain(self.handle_open, protocol, protocol + File "D:\Anaconda\envs\tensorflow\lib\urllib\request.py", line 502, in _call_chain result = func(*args) File "D:\Anaconda\envs\tensorflow\lib\urllib\request.py", line 1397, in https_open return self.do_open(http.client.HTTPSConnection, req, File "D:\Anaconda\envs\tensorflow\lib\urllib\request.py", line 1357, in do_open raise URLError(err) urllib.error.URLError: <urlopen error [WinError 10054] 远程主机强迫关闭了一个现有的连接。> During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:/PYCHARMprojects/Dailypractise/p23.py", line 9, in <module> path = keras.utils.get_file( File "D:\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\keras\utils\data_utils.py", line 282, in get_file raise Exception(error_msg.format(origin, e.errno, e.reason)) Exception: URL fetch failure on https://s3.amazonaws.com/text-datasets/nietzsche.txt: None -- [WinError 10054] 远程主机强迫关闭了一个现有的连接。 Process finished with exit code 1
解决方案:
1、直接在浏览器搜索该网址,右击将其另存到一个地方。
2、修改代码
import keras import numpy as np path = r'D:\PYCHARMprojects\Dailypractise\nietzsche.txt'#文件保存地址
text = open(path).read().lower() print('Corpus length:', len(text))
3、运行成功