1

I am retrieving a record set from a database.
Then using a for statement I am trying to construct my data to match a 3rd party API.

But I get this error and can't figure it out:

"errorType": "TypeError", "errorMessage": "list indices must be integers, not str"
"messages['english']['merge_vars']['vars'].append({"

Below is my code:

cursor = connect_to_database()

records = get_records(cursor)

template = dict()

messages = dict()

template['english'] = "SOME_TEMPLATE reminder-to-user-english"

messages['english'] = {
    'subject': "Reminder (#*|code|*)",
    'from_email': '[email protected]',
    'from_name': 'Notifier',
    'to': [],
    'merge_vars': [],
    'track_opens': True,
    'track_clicks': True,
    'important': True
}

for record in records:

    record = dict(record)

    if record['lang'] == 'english':

        messages['english']['to'].append({
            'email': record['email'],
            'type': 'to'
        })

        messages['english']['merge_vars'].append({
            'rcpt': record['email']
        })

        for (key, value) in record.iteritems():

            messages['english']['merge_vars']['vars'].append({
                'name': key,
                'content': value
            })

    else:

        template['other'] = "SOME_TEMPLATE reminder-to-user-other"

close_database_connection()

return messages

The goal is to get something like this below:

messages = {
 'subject': "...",
 'from_email': "...",
 'from_name': "...",
 'to': [
   {
     'email': '...',
     'type': 'to',
   },
   {
     'email': '...',
     'type': 'to',
   }
 ],
 'merge_vars': [
  {
    'rcpt': '...',
    'vars': [
      {
       'content': '...',
       'name': '...'
      },
      {
       'content': '...',
       'name': '...'
      }
    ]
  },
  {
    'rcpt': '...',
    'vars': [
      {
       'content': '...',
       'name': '...'
      },
      {
       'content': '...',
       'name': '...'
      }
    ]
  }
 ]
}
1
  • Please also shared the content of records Commented Aug 26, 2016 at 15:43

3 Answers 3

2

This code seems to indicate that messages['english']['merge_vars'] is a list, since you initialize it as such:

messages['english'] = {
    ...
    'merge_vars': [],
    ...
}

And call append on it:

messages['english']['merge_vars'].append({
    'rcpt': record['email']
})

However later, you treat it as a dictionary when you call:

messages['english']['merge_vars']['vars']

It seems what you want is something more like:

vars = [{'name': key, 'content': value} for key, value in record.iteritems()]
messages['english']['merge_vars'].append({
    'rcpt': record['email'],
    'vars': vars,
})

Then, the for loop is unnecessary.

Sign up to request clarification or add additional context in comments.

2 Comments

You are right, but this gives me a "list index out of range" error on the same line.
I think I see now what you wanted. Updated answer.
0

What the error is saying is that you are trying to access an array element with the help of string not index (int).

I believe your mistake is in this line:

messages['english']['merge_vars']['vars'].append({..})

You declared merge_vars as array like so:

'merge_vars': []

So, you either make it dict like this:

'merge_vars': {}

Or, use it as array:

messages['english']['merge_vars'].append({..})

Hope it helps

Comments

0

Your issues, as the Error Message is saying, is here: messages['english']['merge_vars']['vars'].append({'name': key,'content': value})

The item messages['english']['merge_vars'] is a list and thus you're trying to access an element when you do something like list[i] and i cannot be a string, as is the case with 'vars'. You probably either need to drop the ['vars'] part or set messages['english']['merge_vars'] to be a dict so that it allows for additional indexing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.