0

I am reading the excel file row by row,and using if condition i am displaying some rows in the file as array.

row =[]
loc = 'output.xlsx'
wb = xlrd.open_workbook(loc) 
sheet = wb.sheet_by_index(0)
for i in range(1,sheet.nrows):
row = sheet.row_values(i)
if row[0] == "A":
print(row)       
else:
print(row)

Actual Output:

[A,gbg,87,89][B,huj,90,65][C,gh,80,32]...[F,uhj,44,21]

Expected output:

[[A,gbg,87,89],[B,huj,90,65],[C,gh,80,32],[....],[....],[....],[F,uhj,44,21]]

Need to save list of array in single array Could anyone please guide me.

Ralf
  • 16,086
  • 4
  • 44
  • 68
  • You can just wrap the row you are printing with []. For eg. [row] – Sudhanshu Bhagwat May 18 '20 at 11:43
  • Please fix your indentations. What is the point of the condition? And you should probably do `row.append(sheet.row_values(i))` I just don't understand the point of your `if` condition – Tomerikoo May 18 '20 at 11:44

4 Answers4

0

You are always reassigning row which is not what you want. That's why you can't save the last value. The append() function is what you want. Here's an example:

rows = []
loc = 'output.xlsx'
wb = xlrd.open_workbook(loc) 
sheet = wb.sheet_by_index(0)
for i in range(1,sheet.nrows):
    row = sheet.row_values(i)
    if row[0] == "A":
        rows.append(row)
print(rows)
ptrstr
  • 56
  • 3
0

You could save the row in an array. result = [] result.append(row) and then print result, like is explained here Python appending array to an array

ldonado
  • 3
  • 3
  • If you believe that this question has an answer somewhere else, [flag](https://stackoverflow.com/questions/61868769/need-to-display-list-of-array-as-single-array-in-python#) it as a duplicate, don't link the answer as a new answer... – Tomerikoo May 18 '20 at 11:50
  • I can't, I don't have enough privilege to do that – ldonado May 18 '20 at 12:00
0

You can do it simply by creating another array, and appending the desired arrays on the go

array_to_print = []
row =[]
loc = 'output.xlsx'
wb = xlrd.open_workbook(loc) 
sheet = wb.sheet_by_index(0)
for i in range(1,sheet.nrows):
    row = sheet.row_values(i)
    if row[0] == "A":
        array_to_print.append(row)       
    else:
        #print(row)  #not understood what you meant to do with it
print(array_to_print)
Himanshu Sheoran
  • 1,266
  • 1
  • 6
  • 5
0

Do you intend to be using the "row" variable twice? You're printing row in your if statement and in the else statement, but I don't think printing is your ultimate goal here. Up top you declare an empty list:

row = []

But later you change that variable to:

row = sheet.row_values(i)

In pythonm, you don't need to declare row as a list at the top, but... if you changed your empty list declaration to something else, like "output_row" you could use it to store your rows:

output_rows = []
loc = 'output.xlsx'
wb = xlrd.open_workbook(loc) 
sheet = wb.sheet_by_index(0)
for i in range(1,sheet.nrows):
  row = sheet.row_values(i)
  if row[0] == "A":
    output_rows.append(row)       
  else:
    print(row) # or whatever
print(output_rows)
Kyle Alm
  • 587
  • 3
  • 14