Bash to Python – Playing with CSV

In the past I have been too willing to use bash scripts for grepping and awking CSV files. The code snippet below is a starter for doing similar with python and while far from perfect, demonstrates calling another system command and manipulating strings:

#!/usr/bin/python

import csv
import os

with open('input.csv') as csv_file:
  csv_reader = csv.reader(csv_file, delimiter=',')

  for row in csv_reader:
    if "filterword" in row[3]:
      field_name = row[2]
      field_dob = row[4]
      field_url = row[3]
      url_parts = row[3].split(".")
      url_region = url_parts.split("-")

      cust_ref = os.popen("/usr/local/bin/somescript.pl %s | grep 'Tier' | awk -F ':' '{print $2}'| tr -d '\n '" % field_name).read()
      print("%s,%s,%s,%s" % (customer_ref,url_region,field_dob,field_url))
    else:
      print("Line does not match expected input")

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>