【发布时间】:2021-12-14 10:28:29
【问题描述】:
在 python 中,我从数据库中收到以下数据结构:
{
"field":"Somestring/somestring.jpg?<<TOKEN>>",
"list": [
{"field": "sometring.html?<<TOKEN>>"},
{"field2": "otherstring.mp4?<<TOKEN>>"}
]
}
而且这个结构很可能会被扩展以包含更多带有字段的列表。
我希望将出现在整个数据结构中不同字符串中的“>”的所有实例替换为其他用于访问这些字符串所代表的 url 的验证令牌。
有没有更好的方法来做到这一点,而不是使用循环访问每个字段,检查其是否为字符串,然后运行:field.replace("<<TOKEN>>", my_token)
【问题讨论】:
-
你能把整个
json加载为字符串,然后在上面加上replace()吗?
标签: python python-3.x dictionary for-loop data-structures