Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

python-3.x - Python分析字典的列表TypeError:字符串索引必须为整数(Python parse list of dictionaries TypeError: string indices must be integers)

I have HTML form input field that returns a list of dictionaries I want to get values from using PYthon in Django View.

(我有HTML表单输入字段,该字段返回字典列表,我希望从Django View中使用PYthon获取值。)

The form returns:

(表单返回:)

request.POST['tags'] returns =>  [{"value":"summer"},{"value":"winter"}]

When I try interating through it using [tag['value'] for tag in request.POST['tags']] to get values, I get TypeError: string indices must be integers .

(当我尝试使用[tag['value'] for tag in request.POST['tags']]遍历以获取值时,出现TypeError: string indices must be integers 。)

However, if I manually copy the input field results to command line and go line by line it works as expected:

(但是,如果我手动将输入字段的结果复制到命令行并逐行进行,它将按预期工作:)

>>> test = [{"value":"summer"},{"value":"winter"}]
>>> test
>>> [{'value':'summer'},{'value':'winter'}]
>>> [tag['value'] for tag in test]
>>> ['summer', 'winter']

What is happening?

(怎么了?)

  ask by curtisp translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...