1

Trying to get the output of the cartesian product in excel using xlsxwriter and arrange them column-wise but have not been successful.

Intended Result:

If the first output is (A,B,C,D,E) the output should be displayed in excel in the following manner:

Row 0, Col 0 = A

Row 0, Col 1 = B

Row 0, Col 2 = C

Row 0, Col 3 = D

Row 0, Col 4 = E

then row+=1 and the next set of results are displayed in the same manner.

Code:

list1 = ['A', 'B', 'C', 'D', 'E', 'F']
list2 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I']
list3 = ['A', 'B', 'C', 'D', 'E']
list4 = ['A', 'B', 'C', 'D']
list5 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I','J','K','L','M']

list = [(p,q,r,s,t) for p in list1 for q in list2 for r in list3 for s in 
        list4 for t in list5]

x = print(list)

import xlsxwriter
workbook = xlsxwriter.workbook('Stat.xlsx')
worksheet = workbook.add_worksheet()

row = 0
col = 0

for Apple, Boy, Cat, Dog, Eagle in (x):
  worksheet.write (row, col, Apple)
  worksheet.write(row, col + 1, Boy)
  worksheet.write(row, col + 1, Cat)
  worksheet.write(row, col + 1, Dog)
  worksheet.write(row, col + 1, Eagle)
  row += 1
workbook.close()
8
  • 1
    did you try see what x contains? When I run your code it's just a string containing <generator object <genexpr> at 0x000001896A293360> which is evidently quite wrong Commented Aug 15, 2018 at 16:59
  • 1
    Aren't you missing the : in the for statement? Commented Aug 15, 2018 at 17:01
  • list = [(p,q,r,s,t) for p in list1 for q in list2 for r in list3 for s in list4 for t in list5] Commented Aug 15, 2018 at 17:04
  • It looks like you aren't incrementing the col value in your for loop. They are all (except Apple) writing to the same column as a result. If you defined column inside the for loop and had an col+=1 in between all of those writes it would put them in different columns. You could also just hardcode the column numbers. Commented Aug 15, 2018 at 17:07
  • Tried with the changes and now it says TypeError: 'module' object is not callable. E1102:xlsxwriter.workbook is not callable. Am I missing something? Commented Aug 15, 2018 at 17:08

1 Answer 1

2

Does this do what you want?

list1 = ['A', 'B', 'C', 'D', 'E', 'F']
list2 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I']
list3 = ['A', 'B', 'C', 'D', 'E']
list4 = ['A', 'B', 'C', 'D']
list5 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I','J','K','L','M']

# You probably could use product from itertools for this: https://docs.python.org/2/library/itertools.html#itertools.product
list_combined = [(p,q,r,s,t) for p in list1 
                             for q in list2 
                             for r in list3 
                             for s in list4 
                             for t in list5]

import xlsxwriter
workbook = xlsxwriter.Workbook('Stat.xlsx')
worksheet = workbook.add_worksheet()

for row, group in enumerate(list_combined):
    for col in range(5):
        worksheet.write (row, col, group[col])
workbook.close()
Sign up to request clarification or add additional context in comments.

4 Comments

Tried running it but it says E1102:xlsxwriter.workbook is not callable.
Looking at the docs, it should be workbook with a capital W. Try my edit
That was amazing. Thank You so much. :)
Hey @Dan can you look into this: stackoverflow.com/questions/51898765/…

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.