python np.loadtxt OSError path not found问题

本文探讨了使用NumPy的loadtxt函数加载本地文件时遇到的路径问题,详细解释了如何正确设置文件路径以避免系统将斜杠视为转义字符,确保数据能够被顺利读取。

import numpy as np
 
file_path = "E:\Desktop file\EMG DATASETS\EMG_data_for_gestures-master\07\file.txt"
data = np.loadtxt(file_path,
                  dtype='float64',
                  skiprows=1)

在这里插入图片描述

在这里插入图片描述
文件路径没有问题,可就是报错:没有找到文件路径
经查阅是因为单斜杠系统会认为是转移符,要是用双斜杠

import numpy as np
file_path = "E:\\Desktop file\\EMG DATASETS\\EMG_data_for_gestures-master\\07\\file.txt"
data = np.loadtxt(file_path,
                  dtype='float64',
                  skiprows=1)

在这里插入图片描述
在这里插入图片描述
不过我之前导入csv文件用的单引号,成功了,所以我也不是很清楚为什么?

--------------------------------------------------------------------------- HTTPError Traceback (most recent call last) Cell In[27], line 13 11 from sklearn.metrics import mean_squared_error, r2_score 12 # 1. 加载加州房价数据集 ---> 13 housing = fetch_california_housing(as_frame=True) 14 df = housing.frame 15 features = housing.feature_names File ~\anaconda3\envs\myenv\Lib\site-packages\sklearn\utils\_param_validation.py:218, in validate_params.<locals>.decorator.<locals>.wrapper(*args, **kwargs) 212 try: 213 with config_context( 214 skip_parameter_validation=( 215 prefer_skip_nested_validation or global_skip_validation 216 ) 217 ): --> 218 return func(*args, **kwargs) 219 except InvalidParameterError as e: 220 # When the function is just a wrapper around an estimator, we allow 221 # the function to delegate validation to the estimator, but we replace 222 # the name of the estimator by the name of the function in the error 223 # message to avoid confusion. 224 msg = re.sub( 225 r"parameter of \w+ must be", 226 f"parameter of {func.__qualname__} must be", 227 str(e), 228 ) File ~\anaconda3\envs\myenv\Lib\site-packages\sklearn\datasets\_california_housing.py:177, in fetch_california_housing(data_home, download_if_missing, return_X_y, as_frame, n_retries, delay) 171 raise OSError("Data not found and `download_if_missing` is False") 173 logger.info( 174 "Downloading Cal. housing from {} to {}".format(ARCHIVE.url, data_home) 175 ) --> 177 archive_path = _fetch_remote( 178 ARCHIVE, 179 dirname=data_home, 180 n_retries=n_retries, 181 delay=delay, 182 ) 184 with tarfile.open(mode="r:gz", name=archive_path) as f: 185 cal_housing = np.loadtxt( 186 f.extractfile("CaliforniaHousing/cal_housing.data"), delimiter="," 187 ) File ~\anaconda3\envs\myenv\Lib\site-packages\sklearn\datasets\_base.py:1512, in _fetch_remote(remote, dirname, n_retries, delay) 1510 while True: 1511 try: -> 1512 urlretrieve(remote.url, temp_file_path) 1513 break 1514 except (URLError, TimeoutError): File ~\anaconda3\envs\myenv\Lib\urllib\request.py:240, in urlretrieve(url, filename, reporthook, data) 223 """ 224 Retrieve a URL into a temporary location on disk. 225 (...) 236 data file as well as the resulting HTTPMessage object. 237 """ 238 url_type, path = _splittype(url) --> 240 with contextlib.closing(urlopen(url, data)) as fp: 241 headers = fp.info() 243 # Just return the local path and the "headers" for file:// 244 # URLs. No sense in performing a copy unless requested. File ~\anaconda3\envs\myenv\Lib\urllib\request.py:215, in urlopen(url, data, timeout, cafile, capath, cadefault, context) 213 else: 214 opener = _opener --> 215 return opener.open(url, data, timeout) File ~\anaconda3\envs\myenv\Lib\urllib\request.py:521, in OpenerDirector.open(self, fullurl, data, timeout) 519 for processor in self.process_response.get(protocol, []): 520 meth = getattr(processor, meth_name) --> 521 response = meth(req, response) 523 return response File ~\anaconda3\envs\myenv\Lib\urllib\request.py:630, in HTTPErrorProcessor.http_response(self, request, response) 627 # According to RFC 2616, "2xx" code indicates that the client's 628 # request was successfully received, understood, and accepted. 629 if not (200 <= code < 300): --> 630 response = self.parent.error( 631 'http', request, response, code, msg, hdrs) 633 return response File ~\anaconda3\envs\myenv\Lib\urllib\request.py:559, in OpenerDirector.error(self, proto, *args) 557 if http_err: 558 args = (dict, 'default', 'http_error_default') + orig_args --> 559 return self._call_chain(*args) File ~\anaconda3\envs\myenv\Lib\urllib\request.py:492, in OpenerDirector._call_chain(self, chain, kind, meth_name, *args) 490 for handler in handlers: 491 func = getattr(handler, meth_name) --> 492 result = func(*args) 493 if result is not None: 494 return result File ~\anaconda3\envs\myenv\Lib\urllib\request.py:639, in HTTPDefaultErrorHandler.http_error_default(self, req, fp, code, msg, hdrs) 638 def http_error_default(self, req, fp, code, msg, hdrs): --> 639 raise HTTPError(req.full_url, code, msg, hdrs, fp) HTTPError: HTTP Error 403: Forbidden
最新发布
10-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值