Documentation

Adding Headers to CSV Data

CSV data from any source can be easily streamed to Alooma via S3. If the CSV file contains headers, select the 'Delimited with header' File Format option when creating the S3 input. Otherwise, select the 'Other' file format, and add the code below to the Code Engine to add field names to each event.

Copy
import csv, io

def add_headers_to_csv(event, headers, key="message", delimiter=",", quotechar="'", pop_key=True):
  """ Return Dictionary with Key/Values from CSV Data """
  message = event[key]
  if pop_key:
    del event[key]

  f = io.StringIO(message)
  reader = csv.reader(f, delimiter=delimiter, quotechar=quotechar)
  csv_data = {}
  fields = list(reader)[0]
  csv_data['data'] = dict(zip(headers[:len(fields)], fields))
  csv_data['num_fields'] = len(fields)
  csv_data['expected_fields'] = len(headers)

  return csv_data

That's it, you're ready to import your header-less CSV data into Alooma!

Search results

    No results found