gpt4 book ai didi

python - 括号内的变量

转载 作者:行者123 更新时间:2023-11-28 22:52:46 26 4
gpt4 key购买 nike

我有这个....

   fileExt = {'png': "'Content-Type': 'image/png'", 'jpe': "'Content-Type':
'image/jpeg'", 'jpg': "'Content-Type': 'image/jpeg'",
'png': "Content-Type': 'image/png'", 'mp4': "'Content-Type': 'video/mpeg'",
'mp3': "'Content-Type': 'audio/mpeg'", '3gp': "'Content-Type': 'video/3gpp'",
'flv': "'Content-Type': 'video/x-flv'", 'flv': "'Content-Type': 'video/x-flv'"}
handler = fileExt.get(ext)
go = "'"+handler
key.copy(key.bucket, key.name, preserve_acl=True, metadata={go})

我收到这个错误...

   Traceback (most recent call last):
File "s3ContentType.py", line 43, in <module>
key.copy(key.bucket, key.name, preserve_acl=True, metadata={go})
File "/task/__pips__/boto/s3/key.py", line 474, in copy
encrypt_key=encrypt_key)
File "/task/__pips__/boto/s3/bucket.py", line 723, in copy_key
headers = boto.utils.merge_meta(headers, metadata, provider)
File "/task/__pips__/boto/utils.py", line 172, in merge_meta
for k in metadata.keys():
AttributeError: 'set' object has no attribute 'keys'

但如果我明确地将一个字符串放在元数据括号内,它就可以工作。

  key.copy(key.bucket, key.name, preserve_acl=True, metadata={'Content-Type': 'image/png'})

给出了什么,我如何在括号内包含一个 var

最佳答案

你在这里构建了两个不同的对象:

foo = {'a', 'b', 'c'}

是一个set , 而

bar = {'a': 'b', 'c': 'd'}

是一个dictionary .您当然可以在构造字典时引用变量:

mimetype = "image/png"
bar = {'Content-Type': mimetype}

字典将键映射到值(因此 :),而集合只是不同值的集合。您调用的函数需要一本字典,因此您必须创建一个。如果你有一本字典并想传递它(从上面继续),你可以这样做:

mimetype = "image/png"
bar = {'Content-Type': mimetype}
key.copy(key.bucket, key.name, preserve_acl=True, metadata=bar)

(请注意,链接指向 python 3 文档,但集合和字典自 python 2 以来并没有真正改变)


对于编辑:正如接受的答案所说,您必须使用嵌套字典。问题是,为什么它在其他情况下不起作用?

否则,您将创建一个集合,其中包含您之前存储在 fileExt 字典中的字符串,例如“Content-Type: 'image/png'”。 Python 不会解释这个字符串,这就是为什么它在您编写 {"Content-Type: 'image/png'""} 时不创建字典的原因。

关于python - 括号内的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20142976/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com