【问题标题】:Array containing 3 arrays will only print one (python)包含 3 个数组的数组只会打印一个(python)
【发布时间】:2018-11-14 19:16:44
【问题描述】:

我试图让我的数组打印它的所有元素,但它只产生一个。我尝试注释掉“for row”部分的不同部分,它表明它们有效。它总是会出一个。任何帮助将不胜感激,谢谢。

            wA = csv.reader(warehouse_A)
            next(wA, None) ## skips the header
            wB = csv.reader(warehouse_B)
            next(wB, None) ## skips the header
            wC = csv.reader(warehouse_C)
            next(wC, None) ## skips the header
            wD = csv.reader(warehouse_D)
            next(wD, None) ## skips the header


            a_itemno = []           ## empty arrays for the item number of the warehouse to be placed
            a_descripton = []               ## empty arrays for the item descripton of the warehouse to be placed
            a_value = []            ## empty arrays for the item value of the warehouse to be placed

            b_itemno = []           
            b_descripton = []               
            b_value = []            

            c_itemno = []           
            c_descripton = []               
            c_value = []            

            d_itemno = []           
            d_descripton = []               
            d_value = []            


            for row in wA:   ## places the 1st row on the spreadsheets into a new array
            a_itemno.append(row[0])

            for row in wB:
            b_itemno.append(row[0])

            for row in wC:
            c_itemno.append(row[0])

            for row in wD:
            d_itemno.append(row[0])


            for row in wA:   ## places the 2rd row on the spreadsheets into a new array
            a_descripton.append(row[1])

            for row in wB:
            b_descripton.append(row[1])

            for row in wC:
            c_descripton.append(row[1])

            for row in wD:
            d_descripton.append(row[1])


            for row in wA:   ## places the 3rd row on the spreadsheets into a new array
            a_value.append(row[2])

            for row in wB:
            b_value.append(row[2])

            for row in wC:
            c_value.append(row[2])

            for row in wD:
            d_value.append(row[2])


            new_wA = [[a_itemno], [a_descripton], [a_value]] ## new array that only prints one element

            print (new_wA)

我正在寻找的输出是: [[[废话,废话,废话,],[废话,废话,废话],[废话,废话,废话]],[[废话,废话,废话,],[废话,废话,废话],[废话,废话] ,废话]],[[废话,废话,废话,],[废话,废话,废话],[废话,废话,废话]]]]

我得到的只是: [[[废话,废话,废话,],[废话,废话,废话],[废话,废话,废话]],[[]],[[]]]

【问题讨论】:

    标签: python arrays csv


    【解决方案1】:

    当您遍历csv.reader 的行时,您正在阅读整个文件,无需倒回。不幸的是,您不能使用 csv.reader 通过使用两个单独的循环遍历整个文件来读取同一个打开的文件两次。

    要读取csv.reader 的行两次,您需要将这些行存储在一个单独的变量中,如下所示:

    wA = csv.reader("warehouse_A")
    wA_list = list(wA)
    
    for row in wA_list:
        a_itemno.append(row[0])
    for row in wA_list:
        a_value.append(row[2])
    

    或者,如果您的文件太大而无法加载到内存中,请考虑修改逻辑并一次性完成您需要的所有操作,例如:

    for row in wA:
        a_itemno.append(row[0])
        a.descripton.append(row[1])
        a_value.append(row[2])
    

    【讨论】:

    • 太好了,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 2022-12-01
    • 2019-12-15
    • 2017-12-27
    相关资源
    最近更新 更多