skip to Main Content

I have following part of list obtained from the csv file (csv downloaded from Shopify).

['#1762', '[email protected]', 'paid', '2020-08-20 09:08:13 +1000', 'fulfilled',
 '2020-08-20 09:27:27 +1000', 'yes', 'AUD', '273.90', '0.00', '24.90', '273.90',
 '""', '0.00', 'Free shipping to your address', '2020-08-20 08:36:29 +1000',
 '1', 'Standard Hand Sanitiser Station - Free-Standing - Black', '273.90',
 '328.90', 'EC-HSS-FS-S-A-B', 'true', 'true', 'fulfilled', 'Luke Smith',
 '211 Riond Rd', '211 Riond Rd', '""', '""', 'Riond', "'5033", 'SA', 'AU',
 '0439 082 558', 'Luke Smith', '211 Riond Rd', '211 Riond Rd', '""', '""',
 'Riond', "'5033", 'SA', 'AU', '043*******58', '""', '""', '',
 'Shopify Payments', 'c1462*******524.1', '0.00', 'Expo Lite', '0.00', '', '',
 '', '264*******220', '""', 'Low', 'web', '0.00', 'GST 10%', '24.90', '', '',
 '', '', '', '', '', '', '', '']

Is there efficient way to get out customer name, email and product purchased?

The issue is that there are number of similar lists in .csv file, but if I use something like this

email = list[1]
name = list[24]
product = list[17]

not the same information obtained from the list and sometimes its item before or after. When I check csv file, the information is all structured correctly and there is no read missing the index.

I’m sorry if question is silly, I cannot wrap my head around this. 🙁

2

Answers


  1. Below is an example of how to do this using the Shopify product_template.csv as an example. This is using DictReader and explicit delimiters+quotechar.

    import csv
    
    with open('product_template.csv') as infile:
        reader = csv.DictReader(infile, delimiter=',', quotechar='"')
        for row in reader:
            print(row['Handle'], row['Vendor'])
    
    Login or Signup to reply.
  2. If the items are always in the same index position in the list, a very good way would be to use operator.itemgetter() to create a function that retrieved all the items at once. It’s efficient in the sense that the setup only has to be done once, and it’s implemented in a highly-optimized built-in module. Here’s what I mean:

    from operator import itemgetter
    
    
    data = ['#1762', '[email protected]', 'paid', '2020-08-20 09:08:13 +1000', 'fulfilled',
     '2020-08-20 09:27:27 +1000', 'yes', 'AUD', '273.90', '0.00', '24.90', '273.90',
     '""', '0.00', 'Free shipping to your address', '2020-08-20 08:36:29 +1000',
     '1', 'Standard Hand Sanitiser Station - Free-Standing - Black', '273.90',
     '328.90', 'EC-HSS-FS-S-A-B', 'true', 'true', 'fulfilled', 'Luke Smith',
     '211 Riond Rd', '211 Riond Rd', '""', '""', 'Riond', "'5033", 'SA', 'AU',
     '0439 082 558', 'Luke Smith', '211 Riond Rd', '211 Riond Rd', '""', '""',
     'Riond', "'5033", 'SA', 'AU', '043*******58', '""', '""', '',
     'Shopify Payments', 'c1462*******524.1', '0.00', 'Expo Lite', '0.00', '', '',
     '', '264*******220', '""', 'Low', 'web', '0.00', 'GST 10%', '24.90', '', '',
     '', '', '', '', '', '', '', '']
    
    data_getter = itemgetter(1, 24, 17)       # Create function.
    email, name, product = data_getter(data)  # Use it.
    print(email, name, product, sep=',')      # Display results.
    

    Output:

    [email protected],Luke Smith,Standard Hand Sanitiser Station - Free-Standing - Black
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search