Skip to content
Advertisement

parsing csv file by python and get it as a list of dictionaries in lambda function

I am trying to write a lambda function that will tag ec2 instances that will go from pending to running state. However, I have a problem reading the csv file that holds my ec2 instance tags. Currently, I have gone to where the lambda returns me the following result.

JavaScript

However, I need a list of dictionaries.

JavaScript

Because the rest of the code looks like the following

JavaScript

At the moment, my lambda code looks like the following

JavaScript

myList.append(row) -> gives me something like this as output

JavaScript

I don’t know how to reach such a state

JavaScript

My cvs file looks like this

tags.csv

And rest of the lambda code

JavaScript

Advertisement

Answer

csv.DictReader returns a dict or an OrderedDict depending on the Python version you are running.

As seen in the documentation:

Changed in version 3.6: Returned rows are now of type OrderedDict.

Changed in version 3.8: Returned rows are now of type dict.

So it seems you are running Python < 3.8 in the lambda function. So you have 2 possibilities to have the output as a dict:

  1. Change your lambda function runtime to a higher one
  2. Convert the OrderedDict to a dict in your code simply using dict method: my_dict = dict(myList)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement