python - 类型错误:+ 不支持的操作数: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' 并根据场景获取输出?
解决方案
您不能使用+
运算符 fordict
和str
。如果要打印两者,请使用逗号分隔符。
print(filejson, j)
或将两者都转换为str
:
print("{} {}".format(filejson, j))
推荐阅读
- c - 哈希函数在删除和替换文件后返回不同的结果
- json - 如何在角度表中打印具有多个值 JSON 数据的键?
- sql - 给定 AWS Athena 中的 Windows 事件管理器事件时间戳,如何使用 SQL 测试 5 分钟间隔
- c++ - 为什么更改 Visual Studio 版本后我的函数会崩溃?
- c# - EF Core 数据库更新指定基本版本
- c - 在最大基础整数值附近使用哨兵指针值是否安全?
- android - 如何使用 gradle uploadArchives 任务上传所有风味 aar 文件?
- reactjs - 我如何一起使用 React 和 xampp?
- python - 使用批处理,如果 Python 脚本关闭,我如何使用批处理使其保持打开状态?
- sql - MS Access查询连续值的最早日期