我在我的 python 代码中定义了一个 URL,我需要读取它并将其保存在 json 文件中,然后将它们保存在列表或字典中。在我的代码中,我阅读并打印它们。另外,我想知道我的代码中是否有任何问题,例如 post 方法或从 URL 读取键值对?
from flask import Flask, jsonify, abort, make_response
from flask_restful import Resource, Api
import requests, json
import urllib.request as request
app = Flask(__name__)
api = Api(app)
data = "given_URL"
myjsondata = {}
@app.route('/my_folder/', methods=["POST"])
@app.error
class read(Resource):
def read_data(self):
with request.urllib.urlopen(metadata) as response:
if response.getcode() == 200:
source = response.read()
data = json.loads(source)
for i in data:
for key, value in i.iteritems():
if key == 'uid':
print key, value
myjsondata[key]=value
return jsonify(myjsondata)
else:
abort(make_response(jsonify(404, massage = "No request found"),400))
api.add_resource(read, '/my_folder')
if __name__ == '__main__':
app.run(debug=True)
读取元数据。以您的方式排列键和值以制作字典。
#Save extracted content to dictionary/list
myjsondata={}
for i in data:
for key, value in i.iteritems():
if key == 'uid':
print key, value
myjsondata[key]=value
#Save your dictionary content to json file
with open('saved_json_file_1.json', 'w') as saveFile:
saveFile.write(json.dumps())
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句