我想从“ td”中提取“ words”字段并将其显示在一行中。如何使用Python达到此效果?谢谢。
td = {'words_result': [{'words': 'a/b/c'},
{'words': '/c/b/a'},
{'words': '/1/2/3'},
{'words': '/3/2/1'}]}
Expectant_effect ='a/b/c/c/b/a/1/2/3/3/2/1'
答案 0 :(得分:1)
从代码看来,这似乎是字典,而不是JSON字符串。所以我在这里有两种方式
如果只是字典- easy peasy
for i in td['words_list']:
print(i, end='')
如果它实际上是JSON字符串-只需再导入一次
import json
jsonString = '{"words_result": [{"words": "a/b/b"}, {"words": "/d/b/a"}, {"words": "1/2/3"}, {"words": "/3/2/1"}]}'
dictString = json.loads(jsonString)
# now same as above
for i in dictString ['words_list']:
print(i, end='')
答案 1 :(得分:0)
您可以使用该代码:
res = []
words_result = td.get('words_result')
for words_dict in words_result:
res.append(words_dict.get('words'))
result = ''.join(res)
print(Expectant_effect == result)
答案 2 :(得分:0)
td = {'words_result': [{'words': 'a/b/c'},
{'words': '/c/b/a'},
{'words': '/1/2/3'},
{'words': '/3/2/1'}]}
line = ''
for item in td['words_result']:
line+= item['words']
#Get YOUR RESULT
print(line)