[解决方法]ValueError: Wrong number of columns at line 2

问题介绍

我用numpy写的一段代码,就是用逗号分隔符读取泰坦尼克号数据.csv文件里的数据:

import numpy as np
with open('泰坦尼克号数据.csv', encoding = 'utf-8') as f:
    csv_data = np.loadtxt(f, str, delimiter=',')
print(csv_data)

点击运行后报错:

ValueError: Wrong number of columns at line 2

问题分析

首先看这句报错是什么意思:第二行列数错误。

然后顺理成章地想到为什么第二行的列数不对呢?是不是数据单元格也含有逗号,在执行delmiter= ','的时候在这个不该切分的地方也切分了呢?

因此去Excel查看,果然在Name这一列有多余的逗号

 这就导致了第二行比第一行多1列,也就导致了第二行列数错误的出现。

解决方法

知道原因后就能解决啦,由于我是为了完成numpy作业才遇到的这个问题,所以我就投机取巧,采取了一个投机的方法:在Excel中,查找并替换逗号(注意:英文格式 , )为冒号(:)

然后把csv文件另存为‘泰坦尼克号数据2.csv’ ,修改代码加载新的csv文件:

import numpy as np
with open('泰坦尼克号数据2.csv', encoding = 'utf-8') as f:
    csv_data = np.loadtxt(f, str, delimiter=',')
print(csv_data)

点击运行后不报错,顺利将csv_data打印了出来:

[['PassengerId' 'Survived' 'Pclass' ... 'Fare' 'Cabin' 'Embarked']
 ['1' '0' '3' ... '7.25' '' 'S']
 ['2' '1' '1' ... '71.2833' 'C85' 'C']
 ...
 ['889' '0' '3' ... '23.45' '' 'S']
 ['890' '1' '1' ... '30' 'C148' 'C']
 ['891' '0' '3' ... '7.75' '' 'Q']]

经验总结

产生这种错误的原因就是出现了列数不等的情况,因此需要根据Wrong number of columns at line (?)问号处提示的行号,定位原始数据中首次出现列数不等错误的行,从而找到造成列数不等的原因。

(mast3r-slam) root@autodl-container-89914fa1bb-a01dbef1:~/MASt3R-SLAM# bash ./scripts/eval_euroc.sh ~/autodl-fs/datasets/euroc/MH_05_difficult/ {'use_calib': True, 'single_thread': True, 'dataset': {'subsample': 2, 'img_downsample': 1, 'center_principle_point': True}, 'matching': {'max_iter': 10, 'lambda_init': 1e-08, 'convergence_thresh': 1e-06, 'dist_thresh': 0.1, 'radius': 3, 'dilation_max': 5}, 'tracking': {'min_match_frac': 0.05, 'max_iters': 50, 'C_conf': 0.0, 'Q_conf': 1.5, 'rel_error': 0.001, 'delta_norm': 0.001, 'huber': 1.345, 'match_frac_thresh': 0.333, 'sigma_ray': 0.003, 'sigma_dist': 10.0, 'sigma_pixel': 1.0, 'sigma_depth': 10.0, 'sigma_point': 0.05, 'pixel_border': -10, 'depth_eps': 1e-06, 'filtering_mode': 'weighted_pointmap', 'filtering_score': 'median'}, 'local_opt': {'pin': 1, 'window_size': 1000000.0, 'C_conf': 0.0, 'Q_conf': 1.5, 'min_match_frac': 0.1, 'pixel_border': -10, 'depth_eps': 1e-06, 'max_iters': 10, 'sigma_ray': 0.003, 'sigma_dist': 10.0, 'sigma_pixel': 1.0, 'sigma_depth': 10.0, 'sigma_point': 0.05, 'delta_norm': 1e-08, 'use_cuda': True}, 'retrieval': {'k': 3, 'min_thresh': 0.005}, 'reloc': {'min_match_frac': 0.3, 'strict': True}, 'inherit': 'config/base.yaml'} Traceback (most recent call last): File "/root/MASt3R-SLAM/main.py", line 170, in <module> dataset = load_dataset(args.dataset) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/root/MASt3R-SLAM/mast3r_slam/dataloader.py", line 325, in load_dataset return EurocDataset(dataset_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/root/MASt3R-SLAM/mast3r_slam/dataloader.py", line 100, in __init__ tstamp_rgb = np.loadtxt(rgb_list, delimiter=",", dtype=np.unicode_, skiprows=0) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/root/miniconda3/envs/mast3r-slam/lib/python3.11/site-packages/numpy/lib/npyio.py", line 1373, in loadtxt arr = _read(fname, dtype=dtype, comment=comment, delimiter=delimiter, ^^^^^^^^^^^^^^^^
最新发布
04-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值