M
M
MaratGan2020-05-03 12:56:19
Python
MaratGan, 2020-05-03 12:56:19

Error string indices must be integers, how to fix?

Good afternoon!

Please tell me what I'm doing wrong, in this code, I need to transfer data in json format:

import json
import sys


def handle(data):
    data = json.loads(data)
    arr1 = data['items']
    arr2 = data['rates']
    arr3 = ''
    companiesDict = { n['id']: { k: n[k] for k in ('name', 'disabled') } for n in arr1 }
    arr3 = [{ **n, **companiesDict.get(n['company'], {}) } for n in arr2 ]
    return arr3


items

[{'id': 14, 'alias': 'maks', 'name': 'Макс', 'disabled': False}, {'id': 4, 'alias': 'rgs', 'name': 'Росгосcтрах', 'disabled': False}, {'id': 12, 'alias': 'vsk', 'name': 'ВСК', 'disabled': False}, {'id': 9, 'alias': 'soglasie', 'name': 'Согласие', 'disabled': False}, {'id': 3, 'alias': 'renins', 'name': 'Ренессанс', 'disabled': False}, {'id': 2, 'alias': 'ingos', 'name': 'Ингосстрах', 'disabled': False}, {'id': 7, 'alias': 'tinkoff', 'name': 'Тинькофф', 'disabled': False}, {'id': 13, 'alias': 'mafin', 'name': 'Mafin', 'disabled': False}, {'id': 8, 'alias': 'ugsk', 'name': 'Югория', 'disabled': False}, {'id': 1, 'alias': 'zetta', 'name': 'Зетта', 'disabled': False}, {'id': 0, 'alias': 'alfa', 'name': 'Альфа', 'disabled': False, 'retentionType': 1}, {'id': 17, 'alias': 'osk', 'name': 'ОСК', 'disabled': False}, {'id': 15, 'alias': 'absolute', 'name': 'Абсолют Страхование', 'disabled': False}, {'id': 16, 'alias': 'euroins', 'name': 'ЕВРОИНС', 'disabled': False}, {'id': 5, 'alias': 'sds', 'name': 'СДС', 'disabled': True}, {'id': 18, 'alias': 'guidehins', 'name': 'Гайде', 'disabled': False}, {'id': 19, 'alias': 'astrovolga', 'name': 'Астро-Волга', 'disabled': False}, {'id': 11, 'alias': 'reso', 'name': 'РЕСО', 'disabled': True, 'retentionType': 1}, {'id': 6, 'alias': 'sngi', 'name': 'Сургутнефтегаз', 'disabled': True}, {'id': 10, 'alias': 'svrezv', 'name': 'Сервис резерв', 'disabled': True}]


rates

[{'company': 0, 'base': 4390, 'price': 3817.32}, {'company': 1, 'base': 4118, 'price': 3580.81}, {'company': 2, 'base': 4560, 'price': 3965.15}, {'company': 3, 'base': 4430, 'price': 3852.11}, {'company': 4, 'base': 4550, 'price': 3956.45}, {'company': 5, 'base': 4000, 'price': 3478.2}, {'company': 6, 'base': 4000, 'price': 3478.2}, {'company': 7, 'base': 4250, 'price': 3695.59}, {'company': 8, 'base': 4000, 'price': 3478.2}, {'company': 9, 'base': 4430, 'price': 3852.11}, {'company': 10, 'base': 4000, 'price': 3478.2}, {'company': 11, 'base': 4118, 'price': 3580.81}, {'company': 12, 'base': 4250, 'price': 3695.59}, {'company': 13, 'base': 4000, 'price': 3478.2}, {'company': 14, 'base': 4000, 'price': 3478.2}, {'company': 15, 'base': 4000, 'price': 3478.2}, {'company': 16, 'base': 4000, 'price': 3478.2}, {'company': 17, 'base': 4000, 'price': 3478.2}, {'company': 18, 'base': 4000, 'price': 3478.2}, {'company': 19, 'base': 4000, 'price': 3478.2}]


Error keeps popping up: TypeError: string indices must be integers

Addendum: Full error description

exit status 1
Traceback (most recent call last):
File "index.py", line 19, in
ret = handler.handle(st)
File "/home /app/function/handler.py", line 30, in handle
exec(code,glob,ldict)
File "", line 14, in
File "", line 10, in handle
File "", line 10, in
File " ", line 10, in
TypeError: string indices must be integers

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zexer, 2020-05-03
@zexer

By the name of the error, it is clear that somewhere not an integer is passed as an index, but something else.
Watch carefully what goes where.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question