[英]How to update a dict value in Python?
Trying to update a dict
value, but somehow getting an error.尝试更新
dict
值,但不知何故出错。
Here is my code:这是我的代码:
print('issue.tags: ', issue.tags)
print(type(issue.tags))
for tag in issue.tags: # Compare eta with tag, to see if changed
print('tag: ', tag)
print(type(tag))
print('tag.get("id", ""): ', tag.get('id', ''))
print(type(tag.get('id', '')))
if ('ETA' in tag.get('id', '')):
try:
oldETA = datetime.datetime.strptime(tag.get('id', '')[4:], '%Y-%m-%d')
except:
oldETA = str(tag.get('id', '')[4:])
if (eta_cet != oldETA):
etaChanged = True
tag['id'][0] = ''.join(['ETA ', str(eta_cet)])
And here is what I get when executing the code:这是我在执行代码时得到的:
issue.tags: [{'id': 'ETA None'}]
<class 'list'>
tag: {'id': 'ETA None'}
<class 'dict'>
tag.get("id", ""): ETA None
<class 'str'>
Traceback (most recent call last):
File "/home/karimbel/ABmktIntel/env/ABmktIntel-1.0/runtime/bin/hello.py", line 11, in <module>
load_entry_point('ABmktIntel==1.0', 'console_scripts', 'hello.py')()
File "/home/karimbel/ABmktIntel/env/ABmktIntel-1.0/runtime/lib/python3.6/site-packages/a_bmkt_intel/hello_world.py", line 64, in main
commentIssueWithETA(results, sim)
File "/home/karimbel/ABmktIntel/env/ABmktIntel-1.0/runtime/lib/python3.6/site-packages/a_bmkt_intel/hello_world.py", line 127, in commentIssueWithETA
tag['id'][0] = ''.join(['ETA ', str(eta_cet)])
TypeError: 'str' object does not support item assignment
Anyone knows if I'm doing something wrong?有谁知道我是否做错了什么?
I want to change the string value from "ETA None" to "ETA + str(eta_cet)".我想将字符串值从“ETA None”更改为“ETA + str(eta_cet)”。 str(eta_cet) is '2019-12-31'.
str(eta_cet) 是“2019-12-31”。 So basically I want to have as a new value of tag id "ETA 2019-12-31".
所以基本上我想拥有一个新的标签 id 值“ETA 2019-12-31”。
If tag
is a dict
then tag['id']
refers to the string 'ETA None'
.如果
tag
是一个dict
则tag['id']
指的是字符串'ETA None'
。
Perhaps your assignment should be tag['id'] = ''.join(['ETA ', str(eta_cet)])
也许你的任务应该是
tag['id'] = ''.join(['ETA ', str(eta_cet)])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.