我正在尝试让我的阵列打印其所有元素,但仅产生一个元素。 ive尝试注释掉“ 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)
我正在寻找的输出将是: [[[[blah,blah,blah,],[blah,blah,blah],[blah,blah,blah]],[[blah,blah,blah,],[blah,blah,blah],[等等,等等,等等],[[等等,等等,],[等等,等等,]]]
所有即时消息都是: [[[[blah,blah,blah,],[blah,blah,blah],[blah,blah,blah]],[[]],[[]]]
答案 0 :(得分:1)
遍历csv.reader
的行时,您正在浏览整个文件,而不会后退。不幸的是,您无法使用csv.reader
通过使用两个遍历整个文件的单独的for循环来两次读取同一打开的文件。
要两次遍历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])