在python中使用for循环提取astropy中的一系列列

2024-06-28 19:06:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下文件:

rowname1     0.0019      0.0142     -0.0258     -0.0058     -0.0097      
rowname2     0.0001      0.0168     -0.0268     -0.0063     -0.0072

我想将第2列(即避免rownames列)提取到第x列。到目前为止,我正在使用astropy模块ascii.read码函数;如设置x=3:

from astropy.io import ascii
x = 3
evec = ascii.read('file', include_names=['col'[i] for i in xrange(2,x)])
print evec

它就回来了

<No columns>

for循环应该如何才能正常工作?你知道吗

谢谢你!你知道吗


Tags: 模块文件函数fromioimportforread
1条回答
网友
1楼 · 发布于 2024-06-28 19:06:19

您在include\u名称中有一个错误,即您选择了“col”的第二个字符,并且没有将数字连接到字符串。你知道吗

替换为:

evec = ascii.read('file', include_names = ['col' + str(i) for i in xrange(2,x)])

还要考虑xrange的第二个参数没有包含在内;因此如果您想在示例中包含3,请使用xrange(2,x+1)。你知道吗

相关问题 更多 >