固定广播输入数组的形状,替换为零

时间:2019-02-16 18:31:32

标签: python arrays numpy multidimensional-array

这里我生成数组形式的txt文件,我想要如果没有数据,那么它将自动替换为零,我该怎么做?

解释我的代码的算法:

代码Moto:我的代码从特定文件夹中获取值,每个文件夹包含一个用户生成的7个“ txt”文件,这样,多个文件夹将包含多个用户的多个数据。

步骤1:启动第一个for循环,并使用特定文件夹中的文件夹数量来控制它,并在变量“ path”中存储第一个文件夹的第一个路径。

第2步:使用2nd for loop打开路径并获取7个txt文件的数据。专长之后,它关闭2nd for loop并执行其余代码。

step3:将7个txt文件的数据合并到一个1d数组中。

第4步:使用获取2个文件夹的数据创建2d数组

第5步:在2d数组中创建一行,然后插入1d数组

import numpy as np
    import os
    import shutil
    #variable that contain inicial path as string
    f_path='Result'

    #for control incriment row index of array with loop
    array_control_var=1


    #create a rarry for storing data of one user, By deufault value zero             
    main_f_array=np.zeros((1,123),int)

    #first row of the array(main_f_array[0]) is label of each question
    main_f_array[0] = np.array([21,22,23,241,242,243,244,245,246,247,248,249,2410,2411,251,252,253,254,255,256,257,
                                258,259,2510,2511,2512,2513,2514,2515,2516,2517,261,262,263,264,265,266,267,
                                268,269,2610,2611,2612,2613,2614,2615,2616,2617,31,32,33,34,35,36,37,38,39,
                                310,311,312,313,314,315,316,317,41,42,43,44,45,46,51,52,53,54,55,56,57,58,59,61,
                                62,63,64,65,66,67,68,69,610,611,612,613,614,615,616,617,618,619,620,621,71,72,
                                73,74,75,76,77,78,79,710,711,712,713,714,715,716,717,718,719,720,721,722])

    #for feacth directory path
    for (path,dirs,file) in os.walk(f_path):
        if(path==f_path):
            continue

        #for count how many file exiest
        file_c=0

        #counting how many file exiext
        file_c += len(file)

        if file_c!=7:

            #if there is not 7 file exiest then delete
            shutil.rmtree(path)
            continue


        f_path_1= path +'\page_1.txt'

        #Get data from page1 indivisualy beacuse there string type data exiest
        pgno_1 = np.array(np.loadtxt(f_path_1, dtype='U', delimiter=','))

        #only for page_2.txt
        f_path_2= path +'\page_2.txt'
        with open(f_path_2) as f:
            str_arr = ','.join([l.strip() for l in f])
        pgno_2 = np.asarray(str_arr.split(','), dtype=int)

        #using loop feach data from those text file data type = int
        for j in range(3,8):

            #store file path using variable
            txt_file_path=path+'\page_'+str(j)+'.txt'


            #genarate a variable name that auto incriment with for loop
            foo='pgno_'+str(j)

            #pass the variable name as string and store value
            exec(foo + " = np.array(np.loadtxt(txt_file_path, dtype='i', delimiter=','))")

        #end for loop

        #marge all array from page 2 to rest in single array in one dimensation
        f_array=np.concatenate((pgno_2,pgno_3,pgno_4,pgno_5,pgno_6,pgno_7), axis=0)

        #assing 1d array(f_array) in row of main_f_array, rwo inciment by array_control_var variable 
        main_f_array=np.insert(main_f_array, array_control_var, f_array, 0)

        array_control_var+=1

        #end for loop

    print(main_f_array)

0 个答案:

没有答案