This script gives you an overview of writing a CSV file with Python. I’m using a dictionary (array data) to populate the sheet
1# -------------------------------------
2# CSV
3# -------------------------------------
4
5# Write these to a CSV file
6filename = 'vivo_phones_shopify.csv'
7
8# vivo_phones_shopify.csv file will be created in the current working
9with open( filename, 'w', newline='') as file:
10 # filednames = a list object which should contain the column headers specifying the order in which data should be written in the CSV file
11 fieldnames = ['Handle','Title','Body (HTML)','Vendor','Type','Tags','Published','Option1 Name','Option1 Value','Option2 Name','Option2 Value','Option3 Name','Option3 Value','Variant SKU','Variant Grams','Variant Inventory Tracker','Variant Inventory Qty','Variant Inventory Policy','Variant Fulfillment Service','Variant Price','Variant Compare At Price','Variant Requires Shipping','Variant Taxable','Variant Barcode','Image Src','Image Position','Image Alt Text','Gift Card','SEO Title','SEO Description','Google Shopping / Google Product Category','Google Shopping / Gender','Google Shopping / Age Group','Google Shopping / MPN','Google Shopping / AdWords Grouping','Google Shopping / AdWords Labels','Google Shopping / Condition','Google Shopping / Custom Product','Google Shopping / Custom Label 0','Google Shopping / Custom Label 1','Google Shopping / Custom Label 2','Google Shopping / Custom Label 3','Google Shopping / Custom Label 4','Variant Image','Variant Weight Unit','Variant Tax Code','Cost per item','Status']
12 writer = csv.DictWriter(file, fieldnames = fieldnames)
13 # writer = csv.writer(file, fieldnames = fieldnames)
14
15 writer.writeheader()
16
17 for title, price, image in zip(titles, prices, images):
18 # Basics
19 title = title.text.strip()
20 price = price.text.strip()
21 image = image.get('src').strip()
22
23 # Shopify defaults
24 handle = title.lower().replace(' ', '_' ) # Required. Can't be blank or missing.
25 # title = '' # Required. Can't be blank or missing.
26 body_html = '' # Required, but can be blank.
27 vendor = 'Vivo' # Required, but can be blank.
28 product_type = 'Smartphone' # Required, but can be blank.
29 tags = '' # Required, but can be blank.
30 published = 'TRUE' # Required. Leaving the field blank publishes the product.
31 option1_name = 'Title' # Required. Can't be blank or missing. For products with only one option, this should be set to 'Title'.
32 option1_value = 'Default Title' # Required. Can't be blank or missing. For products with only one option, this should be set to 'Default Title'.
33 option2_name = '' # Required, but can be blank.
34 option2_value = '' # Required, but can be blank.
35 option3_name = '' # Required, but can be blank.
36 option3_value = '' # Required, but can be blank.
37 variant_sku = '' # Required, but can be blank.
38 variant_grams = '350' # Required. Can't be blank or missing. You must enter a value, even if that value is '0'.
39 variant_inventory_tracker = '' # Required, but can be blank.
40 variant_inventory_qty = '' # Required, but can be blank.
41 variant_inventory_policy = 'continue' # Required. Can't be blank or missing. (values: continue, deny). If the existing inventory tracking options are removed, then inventory is no longer tracked.
42 variant_fulfillment_service = 'manual' # Required. Can't be blank or missing. (values: manual, shipwire, amazon_marketplace_web etc.)
43 variant_price = price # Required. Can't be blank or missing.
44 variant_compare_at_price = '' # Required, but can be blank.
45 variant_requires_shipping = 'TRUE' # Required, but can be blank.
46 variant_taxable = 'FALSE' # Required, but can be blank.
47 variant_barcode = '' # Required, but can be blank.
48 image_src = image # Required, but can be blank.
49 image_position = '' # Required, but can be blank.
50 image_alt_text = '' # Required, but can be blank.
51 gift_card = 'FALSE' # Required, but can be blank.
52 seo_title = '' # Optional (70 chars)
53 seo_description = '' # Optional (320 chars)
54 google_shopping_google_product_category = '' # Optional
55 google_shopping_gender = '' # Optional
56 google_shopping_age_group = '' # Optional
57 google_shopping_mpn = '' # Optional
58 google_shopping_adwords_grouping = '' # Optional
59 google_shopping_adwords_labels = '' # Optional
60 google_shopping_condition = '' # Optional
61 google_shopping_custom_product = '' # Optional
62 google_shopping_custom_label_0 = '' # Optional
63 google_shopping_custom_label_1 = '' # Optional
64 google_shopping_custom_label_2 = '' # Optional
65 google_shopping_custom_label_3 = '' # Optional
66 google_shopping_custom_label_4 = '' # Optional
67 variant_image = '' # Optional
68 variant_weight_unit = 'g' # Optional (default is 'kg')
69 variant_tax_code = '' # Optional. Available to: Shopify Plus plan
70 cost_per_item = '' # Optional
71 status = 'draft' # Required (active, draft, archived)
72
73 # Shopify CSV
74 writer.writerow({
75 'Handle': handle,
76 'Title': title,
77 'Body (HTML)': body_html,
78 'Vendor': vendor,
79 'Type': product_type,
80 'Tags': tags,
81 'Published': published,
82 'Option1 Name': option1_name,
83 'Option1 Value': option1_value,
84 'Option2 Name': option2_name,
85 'Option2 Value': option2_value,
86 'Option3 Name': option3_name,
87 'Option3 Value': option3_value,
88 'Variant SKU': variant_sku,
89 'Variant Grams': variant_grams,
90 'Variant Inventory Tracker': variant_inventory_tracker,
91 'Variant Inventory Qty': variant_inventory_qty,
92 'Variant Inventory Policy': variant_inventory_policy,
93 'Variant Fulfillment Service': variant_fulfillment_service,
94 'Variant Price': variant_price,
95 'Variant Compare At Price': variant_compare_at_price,
96 'Variant Requires Shipping': variant_requires_shipping,
97 'Variant Taxable': variant_taxable,
98 'Variant Barcode': variant_barcode,
99 'Image Src': image_src,
100 'Image Position': image_position,
101 'Image Alt Text': image_alt_text,
102 'Gift Card': gift_card,
103 'SEO Title': seo_title,
104 'SEO Description': seo_description,
105 'Google Shopping / Google Product Category': google_shopping_google_product_category,
106 'Google Shopping / Gender': google_shopping_gender,
107 'Google Shopping / Age Group': google_shopping_age_group,
108 'Google Shopping / MPN': google_shopping_mpn,
109 'Google Shopping / AdWords Grouping': google_shopping_adwords_grouping,
110 'Google Shopping / AdWords Labels': google_shopping_adwords_labels,
111 'Google Shopping / Condition': google_shopping_condition,
112 'Google Shopping / Custom Product': google_shopping_custom_product,
113 'Google Shopping / Custom Label 0': google_shopping_custom_label_0,
114 'Google Shopping / Custom Label 1': google_shopping_custom_label_1,
115 'Google Shopping / Custom Label 2': google_shopping_custom_label_2,
116 'Google Shopping / Custom Label 3': google_shopping_custom_label_3,
117 'Google Shopping / Custom Label 4': google_shopping_custom_label_4,
118 'Variant Image': variant_image,
119 'Variant Weight Unit': variant_weight_unit,
120 'Variant Tax Code': variant_tax_code,
121 'Cost per item': cost_per_item,
122 'Status' : status,
123 })