TypeError: unsupported operand type(s) for +: ‘dict’ and ‘str’ [closed]

  python

I am getting below error on my code :

[ERROR] TypeError: unsupported operand type(s) for +: 'dict' and 'str'
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 19, in lambda_handler
    r = row_w_newline + "n"

I tried replacing  r = row_w_newline + "n" with  row_w_newline = row_w_newline + "n" but still same error

Code :

import json
import boto3
import base64
import string
from typing import Optional, Iterable, Union

delete_dict = {sp_character: '' for sp_character in string.punctuation}
PUNCT_TABLE = str.maketrans(delete_dict)
output = []

def lambda_handler(event, context):
    
    for record in event['records']:
        payload = base64.b64decode(record['data']).decode('utf-8')
        print('payload:', payload)
        
        row_w_newline = json.loads(payload, object_pairs_hook=clean_keys)
        print('row_w_specialchar:', row_w_newline)
        r = row_w_newline + "n"
        print('row_w_newline type:', type(r))
        r = base64.b64encode(r.encode('utf-8'))
        
        output_record = {
            'recordId': record['recordId'],
            'result': 'Ok',
            'data': r
        }
        output.append(output_record)

    print('Processed {} records.'.format(len(event['records'])))
    
    return {'records': output}
    
def strip_punctuation(s: str,
                      exclude_chars: Optional[Union[str, Iterable]] = None) -> str:
    """
    Remove punctuation and spaces from a string.

    If `exclude_chars` is passed, certain characters will not be removed
    from the string.

    """
    punct_table = PUNCT_TABLE.copy()
    if exclude_chars:
        for char in exclude_chars:
            punct_table.pop(ord(char), None)

    # Next, remove the desired punctuation from the string
    return s.translate(punct_table)

def clean_keys(o):
    return {strip_punctuation(k): v for k, v in o}

Source: Python Questions

LEAVE A COMMENT