在Python中指定列的范围

时间:2017-03-08 18:58:39

标签: python arrays database set range

具体来说,我问的是如何从文本文件中获取多个列并将其输入到数组中而不单独指定每个列。

1    2   1    4.151E-12 4.553E-12 4.600E-12 4.852E-12 6.173E-12 7.756E-12 9.383E-12 1.096E-11 1.243E-11 1.379E-11 1.504E-11 1.619E-11 1.724E-11 2.139E-11 2.426E-11 2.791E-11 3.009E-11 3.152E-11 3.252E-11 3.326E-11 3.382E-11 3.426E-11 3.462E-11 3.572E-11 3.640E-11 3.698E-11 3.752E-11
2    3   1    1.433E-12 1.655E-12 1.907E-12 2.014E-12 2.282E-12 2.682E-12 3.159E-12 3.685E-12 4.246E-12 4.833E-12 5.440E-12 6.059E-12 6.688E-12 9.845E-12 1.285E-11 1.810E-11 2.238E-11 2.590E-11 2.886E-11 3.139E-11 3.359E-11 3.552E-11 3.724E-11 4.375E-11 4.832E-11 5.192E-11 5.486E-11

例如,我想将此数据集的第二列单独放在一个数组中,我希望数组中的第三列本身。但是,我希望第四列到数组中的最后一列,这些列由列分隔。我不知道如何在不指定每个列的情况下执行此操作。

1 个答案:

答案 0 :(得分:0)

鉴于您提到了一个文本文件,我会将其视为内容是逐行从文本文件中提取的:

with open("data.txt") as f:
    for line in f:
        data = line.split()

        # I want the second column of this data set in an array by itself
        second_column = data[1]

        # I want the third column in an array by itself
        third_column = data[2]

        # I want column four through the last column in arrays that are separated by column
        fourth_to_last_column = data[3:]

如果您然后打印第一行/输入的second_columnthird_columnfourth_to_last_column,它将如下所示:

2
1
['4.151E-12', '4.553E-12', '4.600E-12', '4.852E-12', '6.173E-12', '7.756E-12', '9.383E-12', '1.096E-11', '1.243E-11', '1.379E-11', '1.504E-11', '1.619E-11', '1.724E-11', '2.139E-11', '2.426E-11', '2.791E-11', '3.009E-11', '3.152E-11', '3.252E-11', '3.326E-11', '3.382E-11', '3.426E-11', '3.462E-11', '3.572E-11', '3.640E-11', '3.698E-11', '3.752E-11']
相关问题