import pandas as pd import pymongo def main(): myclient = pymongo.MongoClient("mongodb://{0}:{1}@10.5.64.238:57017/".format("root","yfkj2024")) db = myclient.smartfarming sa_device = db.sa_device filename = "E:\\2024-云飞科技\\北大荒\\北大荒故障.xlsx" df = pd.read_excel(filename, sheet_name="总表") df = df[["设备号", "北大荒ID", "农场"]] df.fillna("", inplace=True) is_exist = 0 for k in df.to_dict(orient='records'): yf_id = k["设备号"] bdh_id = k["北大荒ID"] farm_name = k["农场"] if bdh_id and farm_name: update = sa_device.update_one( {"device_id": str(yf_id)}, {"$set":{"device_name":farm_name + str(int(bdh_id))}} ) if update.modified_count == 0: print("设备号:", yf_id) print("设备不存在或已更新") else: print("设备号:", yf_id, " 更新成功") else: print("设备号:", yf_id) print("设备号或北大荒ID或农场名称为空") if __name__ == "__main__": main()