[英]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.