简体   繁体   English

如何使用POST请求将发送到Flask的文件解析为json?

[英]How to parse a file sent to Flask with a POST request into json?

I have a python backend to which i send an xml or h5 files from the front. 我有一个python后端,我从前面发送xml或h5文件。 Those xml\\h5 's are huge lists of datapoints about a patient. 那些xml \\ h5是关于病人的巨大数据点列表。 I need to extract certain datapoints(subfields of the object) to feed to the next thing in this pipeline, but am not sure how to operate on a "request" file in python so that it is anything close to a json object im familiar with. 我需要提取某些数据点(对象的子字段)以提供给此管道中的下一个内容,但我不确定如何在python中的“请求”文件上操作,以便它是一个接近于我熟悉的json对象的任何东西。

I have tried xmltodict library for converting the xml to json 我尝试过将xml转换为json的xmltodict库

Say, this is more or less how the POSTed file looks like 比方说,这或多或少是POSTed文件的样子

  <patdata>
      <id>MPS_2782</id>
      <lastname/>

....

And this is the api 这就是api

from flask import jsonify
import requests, os, json, xmltodict
app = Flask(__name__)
api = Api(app)
CORS(app)
#VCG scripts are under Software/Matlab/Transfroms

@app.route('/api/upload', methods = ['POST'])
def upload_file():
    file = request.files['file']    
    contents = xmltodict.parse(file)
    # xmltodict is XML to JSON parser
    print(">> The XML contents. <<<\n")
    print(json.loads(jsonify(contents)))
    return jsonify(contents)

if __name__ == '__main__':
    app.run(debug=False)

How do i get, say, the patient's id for example from 'file' ? 例如,如何从'文件'中获取患者的身份证?

If you are able to convert the XML file to dictionary then it's easy to get different attributes from the dictionary. 如果您能够将XML文件转换为字典,那么很容易从字典中获取不同的属性。

patient_id = contents['patient_id']
print(patient_id)

will fetch you the value. 会取你的价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flask JSON发布请求无效 - Flask JSON post request not working 无法解析通过python / flask中的jQuery通过AJAX发出的JSON POST请求 - Unable to parse JSON POST request made through AJAX with jQuery in python/flask 我如何访问通过 ajax 发送到 php 文件的 POST 请求的 JSON 数据并将它们存储在数组变量中? - how can i acces the JSON data of POST request sent through ajax to a php file and store them in an array variable? 如何从从 javascript 发送到 flask 的发布请求中获取返回值 - How can I get a return value from a post request sent from javascript to flask 如何从从 javascript 发送到 flask 的 post 请求中获取返回值 - How can I get a return value from a post request sent from javascript to flask 发布请求-以json格式发送数据 - Post request - sent data in json format 使用Node.js解析使用NGX-formly和Angular 6通过POST发送的JSON中包含的文件 - Use Node.js to parse file contained in JSON sent via POST using NGX-formly and Angular 6 从 JavaScript 发送 POST 请求后 Flask 不重定向 - Flask not redirecting after POST request sent from JavaScript 如何解析在JSON中以字符串形式发送的POINT对象? - How to parse a POINT object sent as string in JSON? 如何将json对象从POST请求写入/附加到JSON文件? - How to write/append the json object from a POST request to a JSON file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM