在python中从Requests.get创建数组



我执行以下操作:

@app.route('/api/test/', methods=['GET'])
def catalogue1():
 cache = []
 r = requests.get("http://192.168.198.140:5000/api/listdir")
 cache = r.content

缓存 = ["流浪.txt", "Securite_sociale.jpg"] ;

当我尝试打印缓存[0]时,我得到"["。

如何将此结果转换为数组.

我对 Flask 一无所知,但看起来r.content是一个字符串,尚未反序列化为 Python 列表。 例如,而不是

cache = ["vagrant.txt", "Securite_sociale.jpg"]

你有:

cache = '["vagrant.txt", "Securite_sociale.jpg"]' 

我假设它是 JSON,在这种情况下,您可以使用

import json
cache = json.loads(r.content)

但我也假设在 Flask 中有一种内置的方法可以做到这一点

最新更新