基于tornado python pandas和bootstrap上传组件的mongodb数据添加工具
总体思路:基于bootstrap4的前端页面上传组件,把excel文件上传至服务器,并利用python pandas读取里面的数据形成字典列表
通过pymongo 接口把数据插入或追加到mongodb相关集合中
- html部分
<input type="file" id="excelfile" class="form-control"> <div class="form-check form-check-inline"> <input class="form-check-input" type="radio" name="inlineRadioOptions" id="rebuild" value="option1"> <label class="form-check-label" for="rebuild">重建</label> </div> <div class="form-check form-check-inline"> <input class="form-check-input" type="radio" name="inlineRadioOptions" id="append" value="option2"> <label class="form-check-label" for="append">附加</label> </div>
........
<button type="button" id="postbtn" class="btn btn-lg btn-block btn-outline-primary">Sign up for free</button>
- jquery部分:其中24-26行是确保ajax提交成功的关键
1 //radio按键,按下和抬起所代表的不同动作 2 $(".form-check-input").click(function () { 3 if(typeof($(this).attr("checked"))=="undefined") 4 { 5 $(this).attr("checked","checked") 6 } 7 else 8 { 9 $(this).removeAttr('checked') 10 } 11 }); 12 13 $("#postbtn").click(function () { 14 //根据选中的radio,执行相应的操作,追加mongodb的数据集或是 15 //删除重建 16 var param = $(".form-check-input:checked").attr("id"); 17 //alert("参数"+param); 18 var dataform = new FormData(); 19 dataform.append('fileobj',$("#excelfile")[0].files[0]); 20 dataform.append('status',param); 21 $.ajax({ 22 url:'/qingxiniaodao', 23 type:'post', 24 mimeType:'multipart/form-data', 25 contentType:false, 26 processData:false, 27 data:dataform, 28 success:function (data) { 29 console.log(data) 30 } 31 }) 32 }); 33 </script>
- tornado----python部分
class qingxiniaodao(RequestHandler): def get(self): self.render('mongowithpanda.html') def post(self): if self.request.files: #第一步上传文件 filename = self.request.files['fileobj'][0]['filename'] param =self.get_argument("status")#如果值为rebuild就重建,append为附加 filename =currentpath+"\\temfile\\"+filename with open(filename,"wb") as writer: writer.write(self.request.files['fileobj'][0]['body']) #第二步:读取文件生成dataframe mydataframe = pandas.read_excel(filename) print(type(mydataframe.to_json(orient='records'))) self.write(json.dumps({"rets": mydataframe.to_json(orient='records')})) #第三步获取集合名称----默认每个excel文件只有一个sheet collectioname = pandas.ExcelFile(filename) #获取sheet名 print(collectioname.sheet_names[0]) #mongodb操作: client = MongoClient("localhost",27017) db = client['olddream'] cols = db[collectioname.sheet_names[0]] if(param=="rebuild"): cols.remove() title = [] rets = [] superdict = {} retlist = [] print("这是表头:", mydataframe.columns) for i in mydataframe.columns: title.append(i) # mydataframe.values----结果集ndarray类型 print("算出数据集的行,列数:", mydataframe.values.shape) rowcount = mydataframe.values.shape[0] # ndarray的行数,--元组 i = 0 print("打印数据集的值:", mydataframe.values) print("打印数据集的某行某列:", mydataframe.values[0][2]) k = 0 for i in range(0, rowcount): for k in range(0, len(title)): superdict[title[k]] = mydataframe.values[i][k] retlist.append(superdict) superdict = {} i += 1 self.write(json.dumps({"rets": retlist})) insertrows=cols.insert_many(retlist) self.write(json.dumps({"rets":str(insertrows.inserted_ids)}))