update_device_name.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import pandas as pd
  2. import pymongo
  3. def main():
  4. myclient = pymongo.MongoClient("mongodb://{0}:{1}@10.5.64.238:57017/".format("root","yfkj2024"))
  5. db = myclient.smartfarming
  6. sa_device = db.sa_device
  7. filename = "E:\\2024-云飞科技\\北大荒\\北大荒故障.xlsx"
  8. df = pd.read_excel(filename, sheet_name="总表")
  9. df = df[["设备号", "北大荒ID", "农场"]]
  10. df.fillna("", inplace=True)
  11. is_exist = 0
  12. for k in df.to_dict(orient='records'):
  13. yf_id = k["设备号"]
  14. bdh_id = k["北大荒ID"]
  15. farm_name = k["农场"]
  16. if bdh_id and farm_name:
  17. update = sa_device.update_one(
  18. {"device_id": str(yf_id)},
  19. {"$set":{"device_name":farm_name + str(int(bdh_id))}}
  20. )
  21. if update.modified_count == 0:
  22. print("设备号:", yf_id)
  23. print("设备不存在或已更新")
  24. else:
  25. print("设备号:", yf_id, " 更新成功")
  26. else:
  27. print("设备号:", yf_id)
  28. print("设备号或北大荒ID或农场名称为空")
  29. if __name__ == "__main__":
  30. main()