Compare csv files with Python
I used this script to compare two different SKU CSV lists with python.
The script is checking the SKU numbers and then filling in the right prices for the webshop.
Script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import csv price=open("sales_sku.csv","rb") #Sales database data2=csv.reader(price) shop=open("webshop_sku.csv","rb") # Webshop database data=csv.reader(shop) row2 = data.next() arr1 = []; for sku in data: arr1.append(sku); arr2 = []; for sku in data2: arr2.append(sku); lonely_sku = [] webshop_sku = {} def gen(test): for sku in arr2: if sku[0] == test[0]: return sku; for test in arr1: row = gen(test); if(row == None): lonely_sku.append(test[0]) else: webshop_sku[row[0]] = (row[11]) price.close() shop.close() out=open("output.csv","wb") output=csv.writer(out) for key, value in webshop_sku.iteritems(): data = key, value output.writerow(data) out.close() out_lonely=open("lonely_sku.csv","wb") output_lonely=csv.writer(out_lonely) for sku in lonely_sku: output_lonely.writerow([sku]) out_lonely.close() |
Webshop_sku.csv
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
sku special_price 7 11 12 18 44 74 81 107 109 110 112 114 116 118 138 156 166 176 |
Sales_SKU.csv
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
7 PED SCHMACKOS RUND 9X104GR 10.08 1.62 10.08 1.12 21 ? 0.22 20% 4 1.29 1.56 11 SHEBA PATE MET RUND 16X100GR 9.94 0.94 9.94 0.62 21 ? 0.16 25% 6 0.74 0.9 12 SHEBA PATE MET KIP 16X100GR 9.94 0.94 9.94 0.62 21 ? 0.16 25% 6 0.74 0.9 18 WHIS JUNIOR RUND 24X195GR 15.93 0.92 15.93 0.66 21 ? 0.10 15% 4 0.76 0.92 44 KUIKENS VERPAKT 12X1000GR 16.2 3.79 16.2 1.35 6 ? 2.23 165% 21 2.16 2.29 74 PED BISCROK KIP 10KG 25.89 44.54 25.89 25.89 21 ? 10.92 42% 9 33.66 40.72 81 PED MARKIES 12,5KG 27.96 53.13 27.96 27.96 21 ? 15.95 57% 12 37.75 45.67 107 PETSN DS GEP BOT 12'' 9X5X450G 184.5 5.75 184.5 4.1 21 ? 0.65 16% 4 4.72 5.71 109 PETSN DS MUN ROOD 5'' 20X100X9 79.6 6.25 79.6 3.98 21 ? 1.19 30% 6 4.78 5.78 110 PETSN DS MUN MIX 5'' 20X100X9- 79.6 3.8 79.6 3.98 21 ? -0.84 -21% 1 4.18 5.06 112 PETSN DS GEKN BOT 5'' 20X10ST 117.8 1.55 117.8 0.59 21 ? 0.69 117% 21 0.94 1.14 114 PETSN DS GEKN BOT 15'' 4X10X24 112.25 5.95 112.25 2.81 21 ? 2.11 75% 15 4.07 4.93 116 PETSN DS GEP STAAF 10'' 20X10X 176.35 1.95 176.35 0.88 21 ? 0.73 83% 17 1.32 1.6 118 PETSN DS KAUWSCH 8'' 4X10X20CM 51.86 2.05 51.86 1.3 21 ? 0.39 30% 7 1.63 1.97 138 PETSN DS TWIST ST 5'' 24X100X7 198.21 18.9 198.21 8.26 21 ? 7.36 89% 18 12.39 14.99 |
The script takes the L row from the csv file, in this case this is the last value which starts with 1.56.
Output.csv
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
7 1.56 11 0.9 12 0.9 18 0.92 44 2.29 74 40.72 81 45.67 107 5.71 109 5.78 110 5.06 112 1.14 114 4.93 116 1.6 118 1.97 138 14.99 |
Lonely_sku.csv
1 2 3 |
156 166 176 |
The numbers in the Lonely_sku are the numbers that were not matching between the webshop_sku and the sales_sku file.
Download Now –> [download id=”9″] <–
Leave a Reply