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()
xcontains? When I run your code it's just a string containing<generator object <genexpr> at 0x000001896A293360>which is evidently quite wrong:in theforstatement?colvalue in yourforloop. They are all (except Apple) writing to the same column as a result. If you defined column inside theforloop 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.