または評価なし:
t = {"tags" : "['x', 'y', 'z']"}
tags = [el.replace("'","").strip() for el in t['tags'][1:-1].split(',')]
# Basic string splitting:
tags = t['tags'].split(',')
# To replace a character in a string, like "z"
"a123".replace("a", "b") => "b123
# To strip whitespace:
" Wow ".strip() => "Wow"
# Then, a list comprehension to loop through elements of an array and put them in new array:
x = [1, 2, 3]
y = [i+1 for i in x] => [2, 3, 4]
# All together, this becomes
tags = [el.replace("'","").strip() for el in t['tags'][1:-1].split(',')]
eval はコード インジェクションの影響を受けやすく、予測できないため、悪であると言う人もいます。しかし、入力を信頼している限り、問題はありません。を使用するast.literal_eval
と、基本的な型に対してのみ評価されるため、eval よりもはるかに優れているため、コード インジェクションについて心配する必要はありません。