python - 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
解决方案
您正在设置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)
只是猜测这就是你的意图。
推荐阅读
- apache-kafka - 使用 Pglogical 将 PostgreSQL 复制到 Kafka
- reactjs - 如何使用 NPM 构建分叉节点模块?
- function - Powershell:如何从 foreach 循环内部调用函数?
- c++ - Correct way of initialing an array within a struct in C++
- java - 顶点数组到浮点数组
- php - 用自定义 div 替换 wordpress 博客文章图像标记
- c - 我想阻止浮点变量存储 . “点”和任何负数本身,因为这会在稍后创建一个值,即 -10000000
- ios - 动作按钮在 SwiftUI 中无法正常工作
- django - 在我的 jinja2 模板中使用 url 作为变量?
- shopify - Shopify,在收藏页面上显示第一个库存产品变体的价格