一些i/o问题

2024-04-19 19:31:31 发布

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

免责声明:这是一个任务。如果你觉得我只是“让你帮我做作业”,请告诉我,我会问一个更宽泛的问题,或者如果你愿意的话,给我一些提示。你知道吗

好的,我有两套100个文件。第一组称为cell\u spks\n,其中n=1,…,100;第二组称为cell\u dirs\n,其中n=1,…,100。numpy的loadtxt以5x8数组加载这些文件,这是完美的。我想把这些都装起来,对它们做点什么。现在我的问题是给所有这些文件命名。我想制作两个名为dir和spk的列表,并将数组按顺序存储在其中。但是出了问题,它只附加了一个numpy加载的元素,我不确定出了什么问题。你知道吗

from numpy import *

files = 100
for i in range(1, files+1):
    dirs = []
    spks = []
    if (0<i<9):
        dirs_name = 'neurondata/cell_dirs_00' + str(i) + '.txt' 
        spks_name = 'neurondata/cell_spks_00' + str(i) + '.txt'
        dirs.append(loadtxt(dirs_name))
        spks.append(loadtxt(spks_name))
    elif (9<i<=99):
        dirs_name = 'neurondata/cell_dirs_0' + str(i) + '.txt' 
        spks_name = 'neurondata/cell_spks_0' + str(i) + '.txt'
        dirs.append(loadtxt(dirs_name))
        spks.append(loadtxt(spks_name))
    else:
        dirs.append(loadtxt('neurondata/cell_dirs_100.txt'))
        spks.append(loadtxt('neurondata/cell_spks_100.txt'))


# Fancy stuff gets done here

我认为将这些作为数组加载可能是个坏主意,我必须注意索引以访问数据。理想的情况是有这样的循环:

for i in range(1,files+1):
    spk_i = loadtxt('cell_spks_i')
    dir_i = loadtxt('cell_dirs_i')

有什么想法?你知道吗

编辑:我忘记了一些输出

如果我说

for item in spks:
    print item
print shape(spks)

我得到了输出

[[ 25.287356   23.655914   22.988506   14.285714    2.3809524   4.3478261
19.354839   11.764706 ]
[ 16.129032   26.666667   19.565217    7.2289157   5.8823529  13.861386
7.0588235  12.195122 ]
[ 13.157895   16.86747    26.190476   29.62963    12.121212   12.307692
27.5        19.047619 ]
[ 18.518519   25.396825   34.482759   14.814815   20.224719    9.4117647
6.6666667  21.686747 ]
[ 32.55814    22.988506   26.506024   21.782178   13.114754    2.7777778
14.814815    8.6021505]]
(1, 5, 8)

Tags: 文件nameinnumpytxtforcellfiles