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.

2
  • You can just wrap the row you are printing with []. For eg. [row] Commented May 18, 2020 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 Commented May 18, 2020 at 11:44

4 Answers 4

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)
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

If you believe that this question has an answer somewhere else, flag it as a duplicate, don't link the answer as a new answer...
I can't, I don't have enough privilege to do that
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)

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.