异常:TypeError(字符串索引必须为整数) [英] Exception: TypeError(string indices must be integers)

查看:25
本文介绍了异常:TypeError(字符串索引必须为整数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了下面的python函数(完整代码的一小段)来在AWS Lambda中工作。其目的是从S3存储桶中获取一个GeoJSON并进行相应的解析。

解析后,将其放回JSON格式(data),然后使用

将其插入到指定的数据库中
        
                    bulk_item['uuid'] = str(uuid.uuid4())
                    bulk_item['name'] = feature_name
                    bulk_item['type'] = feature_type
                    bulk_item['info'] = obj
                    bulk_item['created'] = epoch_time
                    bulk_item['scope'] = 2

                    data = json.dumps(bulk_item)
                    print(data)
                    
           
                    self.database.upsert_record(self.organisation, json_doc=data)  

                
        except Exception as e:
            print(f'Exception: {e.__class__.__name__}({e})')
与上述内容相关的db_access文件是另一个python脚本。函数upsert_record如下:

def upsert_record(self, organisation,
        json_doc={}):

我的代码运行良好,直到我尝试将其插入到数据库中。一旦找到此行,它就会抛出错误

Traceback (most recent call last):
File "/var/task/s3_asset_handler.py", line 187, in process_incoming_file
self.database.upsert_record(self.organisation, json_doc=data)
File "/opt/python/database_access.py", line 1218, in upsert_record
new_uuid = json_doc['uuid']
TypeError: string indices must be integers

我似乎根本想不出这个问题

推荐答案

您正在尝试从Json对象获取元素,但传递的是字符串。

data = json.dumps(bulk_item)

创建表示对象的字符串。

尝试单独使用bulk_item

这篇关于异常:TypeError(字符串索引必须为整数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆