1

I'm writing a simple Python script which will take a json formatted config file, apply it to a template file (load the file as a string, then apply the string.format method), and then write the output to an output file.

The config file looks a little like this (JSON):

{
    "param1": "param",
    "param2": [
        "list1": "list item 1",
        "list2": "list item 2"
    ]
}

The template file looks a little like this (plain text):

Param1={param1}
List1={param2[0].list1}
List2={param2[0].list2}

And the python script looks a lot like this (Python):

#! /usr/bin/env python
import sys, json

if len(sys.argv) < 4:
    sys.exit("Invalid arguments")

config_file = open(sys.argv[1])
config_json = json.loads(config_file.read())
config_file.close()

template_file = open(sys.argv[2])
template_data = template_file.read()
template_file.close()

output_data = template_data.format(**config_json)

output_file = open(sys.argv[3], 'w')
output_file.write(output_data)
output_file.close()

The Python interpreter errors with the following:

AttributeError: 'dict' object has no attribute 'list1'

Can someone explain to me how to fix this problem? If possible I would like to avoid changing the Python script and favour the correct syntax for referencing the properties in the param2 array of objects, if such a syntax exists.

blockchaindev
  • 3,134
  • 3
  • 22
  • 30

0 Answers0