首页 > 解决方案 > 类型错误:+ 不支持的操作数:python 中的“dict”和“str”

问题描述

我在 AWS Lambda 中编写代码。我需要读取一个 json 文件,并取决于需要从该 json 文件中捕获不同值的数据。请找到以下代码

    fileobj =s3.get_object(Bucket=srcbucname, Key=uploadfilename)
    filedata =fileobj["Body"].read().decode('utf-8')
    filejson = json.loads(filedata) # holds json file data
    print(filejson)
    dict1 = {"orders":["['items'][0]['ordernumber']","['items'][0]['name']"],"transaction":["['items'][0]['trans_id']"]}
    fileperson = filejson['items'][0]['name'] #Depends on what json file contains, need to pull desired details
    print(fileperson)
    key = dict1[fileperson]
    for i in key:
        j = i
        print(filejson+j)   # Here im getting [ERROR] TypeError: unsupported operand type(s) for +: 'dict' and 'str'    

    print("Details provided")

您能否为我提供解决方案如何解决 [ERROR] TypeError: unsupported operand type(s) for +: 'dict' and 'str' 并根据场景获取输出?

标签: pythonpython-3.xtypeerror

解决方案


您不能使用+运算符 fordictstr。如果要打印两者,请使用逗号分隔符。

print(filejson, j)

或将两者都转换为str

print("{} {}".format(filejson, j))

推荐阅读