Created
September 15, 2016 12:10
-
-
Save sarathlal-old/11d178151dea0e10f2339d664c31151e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def all_same(items): | |
| return all(x == items[0] for x in items) | |
| import openpyxl | |
| import csv | |
| wb = openpyxl.load_workbook('output_beautify.xlsx') | |
| #wb.get_sheet_names() | |
| sheet = wb.get_sheet_by_name('Sheet1') | |
| row_count = sheet.max_row | |
| col_count = sheet.max_column | |
| #print row_count | |
| #print col_count | |
| f = open('output_master.csv','w') | |
| # Add Title row on first line | |
| for x in range(1,col_count+1): | |
| cell_title = str(sheet.cell(row=1, column=x).value) | |
| f.write(cell_title) | |
| if (x == col_count): | |
| f.write("\n") | |
| else: | |
| f.write(", ") | |
| # end Title row | |
| #initialize old_unique_identifier variable to check if it is a new product or product varient | |
| old_unique_identifier = "" | |
| smallArray = [] | |
| bigArray = [] | |
| colorArray = [] | |
| sizeArray = [] | |
| for i in range(2, row_count + 1): | |
| #select a unique idetifier for the product | |
| cur_unique_identifier = str(sheet.cell(row=i, column=4).value) | |
| #print ("cur = " + cur_unique_identifier) | |
| if(cur_unique_identifier != old_unique_identifier): | |
| #print("This is a new line") | |
| if bigArray: | |
| #print(bigArray) | |
| #clear size and color array | |
| colorArray = [] | |
| sizeArray = [] | |
| # fix the options as coloumn 12th (11th index) and 14th(13th index) of exel. So find variant and options | |
| #Color Array | |
| for j in range(len(bigArray)): | |
| colorArray.append(bigArray[j][11]) | |
| #print(colorArray) | |
| #remove duplicate in array | |
| NewcolorArray = list(set(colorArray)) | |
| #Size Array | |
| for j in range(len(bigArray)): | |
| sizeArray.append(bigArray[j][13]) | |
| #print(sizeArray) | |
| #remove duplicate in array | |
| NewsizeArray = list(set(sizeArray)) | |
| firstArray = bigArray[0] | |
| for q in range(len(firstArray)): | |
| texttoinsert = str(firstArray[q]) | |
| f.write(texttoinsert + ",") | |
| #write features | |
| f.write('"' + ','.join(map(str, NewsizeArray)) + '",') | |
| f.write('"' + ','.join(map(str, NewcolorArray)) + '"') | |
| f.write("\n") | |
| #clear arrays | |
| bigArray = [] | |
| smallArray = [] | |
| for m in range(1, col_count + 1): | |
| #print(sheet.cell(row=1, column=m).value , sheet.cell(row=i, column=m).value) | |
| string = str(sheet.cell(row=i, column=m).value) | |
| smallArray.append(string) | |
| else: | |
| smallArray = [] | |
| varien = False | |
| #print("Not a new line") | |
| for m in range(1, col_count + 1): | |
| #print(sheet.cell(row=1, column=m).value , sheet.cell(row=i, column=m).value) | |
| string = str(sheet.cell(row=i, column=m).value) | |
| smallArray.append(string) | |
| ''' | |
| if (m == col_count): | |
| f.write(string + "\n") | |
| else: | |
| f.write(string + ", ") | |
| ''' | |
| if smallArray: | |
| bigArray.append(smallArray) | |
| # Move cur_unique_identifier to old_unique_identifier | |
| old_unique_identifier = cur_unique_identifier | |
| #print("old = " + old_unique_identifier) | |
| f.close() | |
| ''' | |
| f = open('myfile','w') | |
| f.write('hi there\n') # python will convert \n to os.linesep | |
| f.close() | |
| ''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment