savedetomongo.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import pymongo
  2. import pandas
  3. import json
  4. from pymongo import MongoClient
  5. client = MongoClient("mongodb://owner:Pheecian1@47.101.198.30:27017/dinge")
  6. #client = MongoClient()
  7. db = client["dinge"]
  8. collection = db["de-collection"]
  9. from subdir import service
  10. ##print(collection.find_one({"DEBH": "3-94"}))
  11. def handle_cl(cl):
  12. result = {}
  13. result["CLBH"] = cl["CLBH"]
  14. result["CLMC"] = cl["CLMC"]
  15. result["JLDW"] = cl["JLDW"]
  16. result["YSJG"] = cl["YSJG"]
  17. result["SL"] = cl["SL"]
  18. result["HJ"] = cl["HJ"]
  19. return result
  20. def handle_rg(rg):
  21. result = {}
  22. result["CLBH"] = rg["CLBH"]
  23. result["CLMC"] = rg["CLMC"]
  24. result["JLDW"] = rg["JLDW"]
  25. result["YSJG"] = rg["YSJG"]
  26. result["gr"] = rg["gr"]
  27. result["gf"] = rg["gf"]
  28. return result
  29. def handle_jx(jx):
  30. result = {}
  31. result["jxbh"] = jx["jxbh"]
  32. result["jxmc"] = jx["jxmc"]
  33. result["DW"] = jx["DW"]
  34. result["tbdj"] = jx["tbdj"]
  35. result["sl"] = jx["sl"]
  36. result["hj"] = jx["hj"]
  37. return result
  38. df = pandas.read_csv("de/tj/DingE_Processedtj.csv", dtype='str')
  39. for i in range(len(df)):
  40. d = df.iloc[i].to_dict()
  41. if '20-P' in d['DEBH']:
  42. print(d)
  43. A1, A2, rg, jx, cl, bz_, A3 = service.getSingleDeXilie_tj(d["DEBH"])
  44. if bz_:
  45. bz = json.loads(bz_)
  46. mongo_bz = []
  47. for key in bz["ID"].keys():
  48. mongo_bz.append({
  49. "BZBH" : bz["BZBH"][key],
  50. "SM": bz["SM"][key],
  51. "THMC" : bz["THMC"][key]
  52. })
  53. else:
  54. mongo_bz = []
  55. mongo_rg = [handle_rg(entry) for entry in rg ]
  56. mongo_jx = [handle_jx(entry) for entry in jx ]
  57. mongo_cl = [handle_cl(entry) for entry in cl ]
  58. d[ "mongo_bz"]= mongo_bz
  59. d["mongo_rg"]= mongo_rg
  60. d["mongo_cl"]= mongo_cl
  61. d["mongo_jx"]= mongo_jx
  62. print(d)
  63. collection.insert_one(d)