首页 > 解决方案 > IndexError:索引 29 超出轴 1 的范围,大小为 14

问题描述

我不知道为什么我的镜头无法读取所有列。我打印 len(data) 但它只读取 15 而不是 39。我怎样才能让它读取所有列。

import numpy as np
import warnings

#warnings.simplefilter('always',Warning)


############################### start: define here ###################################
kkfile      = "scan_run_01.txt"     
outfile ="masslist.out"

############################### end: define here ###################################
n_cols = len(kkfile)

Omega =np.loadtxt(kkfile,usecols=np.arange(1, n_cols))[:,1]
LR =np.loadtxt(kkfile,usecols=np.arange(1, n_cols))[:,0]        
tot_sigmav=np.loadtxt(kkfile,usecols=np.arange(1, n_cols))[:,29]
BB_sigmav=np.loadtxt(kkfile,usecols=np.arange(1, n_cols))[:,23]
UU_sigmav=np.loadtxt(kkfile,usecols=np.arange(1, n_cols))[:,18]

print(n_cols)

line1='  Omega     LR    Tot_sigmav    BB_sigmav  UU_sigmav  mass '

np.savetxt(outfile,np.c_[Omega,LR,tot_sigmav,BB_sigmav,UU_sigmav],'%25.15e', header=line1)

print "\nextracted mass had been print to'%s'."%outfile

标签: python

解决方案


您正在设置n_cols字符串“scan_run_01.txt”的长度。我不知道 scan_run_01.txt 是什么样子,因为您没有包含它,但是使用文件名的长度作为列数肯定不是您真正想要的。大概你想要kkfile中数据的长度?或者第一行的长度?或者也许第一行的长度被一些分隔符分割?

with open(kkfile, 'r') as stream:
   content = stream.read()

# Change this to get the length of what you actually want.
n_cols = len(content)

只是猜测这就是你的意图。


推荐阅读