Python - read numbers from text file and put into list

前端 未结 6 862
日久生厌
日久生厌 2020-12-03 16:12

So like the title says im starting to learn some python and im having trouble picking up on this technique. What I need to accomplish is to read in some numbers and store t

6条回答
  •  眼角桃花
    2020-12-03 16:52

    I do not recommend using append for a big array. It's 50 time slower than creating a zero array and assigning values to it.

    import numpy
    fname = "D:\Test.txt";
    num_lines = sum(1 for line in open(fname));
    array = numpy.zeros((num_lines,4));
    k = 0;
    with open(fname, "r") as ins:
        for line in ins:
            a =[int(i) for i in line.split(' ')];;
             array[k,0:4] =a;
             k = k+1;
    print(array)
    

提交回复
热议问题