• Python 存储数据到数据库


    数据:

    {
        "ResultCount": 2,
        "Code": "200",
        "DealResult": "[{\"HospitalID\":1385899,\"HospitalCode\":\"H108801565\",\"ExternalID\":\"H108801565\",\"HospitalName\":\"浙商银行股份有限公司医务室\",\"RegionId\":23,\"Region\":\"浙江省\",\"CityId\":\"773\",\"City\":\"杭州市\",\"HospitalGradeId\":0,\"HospitalGrade\":null,\"HospitalTypeId\":0,\"HospitalType\":null,\"StopStatusId\":1,\"StopStatus\":\"启用\",\"StatisticsPropertyId\":0,\"StatisticsProperty\":null,\"CountyId\":0,\"County\":null,\"HospitalPropertyId\":0,\"HospitalProperty\":null,\"CustomerAddress\":\"杭州市下城区庆春路288号\",\"HospitalClassificationId\":null,\"HospitalClassification\":null,\"HospitalGradeUnit\":0,\"DoctorNum\":null,\"CertifiedDoctorNum\":0,\"SpecialistNum\":0,\"SectionOfficeNum\":0,\"BedNum\":null,\"DayOutpatientNum\":0.0,\"YearPurchaseAmount\":0.0,\"ExistingProducts\":null,\"TelePhone\":null,\"ZipCode\":null,\"WebSite\":null,\"LegalPerson\":null,\"Canton\":null,\"IsCountyLevelMedicalId\":0,\"IsCountyLevelMedical\":null,\"ProviderRegisterNO\":null,\"OldName\":null,\"Remark\":\"H108801565\",\"ChannelId\":1,\"Channel\":\"生产企业\",\"CreateUserId\":2003610,\"CreateUser\":\"系统管理员03\",\"CreateTime\":\"2022-11-04 16:27:44\",\"UpdateUserId\":2022302,\"UpdateUser\":\"admingww\",\"UpdateTime\":\"2023-09-26 17:18:50\",\"strHospitalMain\":null,\"strDoctor\":[{\"DoctorID\":1062435,\"Doctor\":\"1\",\"DoctorName\":\"1\",\"DoctorCode\":\"1\",\"PhoneNum\":\"1\",\"PositionName\":\"院长\",\"SectionOfficeName\":\"老干部科\",\"HospitalPhoneNum\":null,\"SectionOffice\":9,\"POSITION\":10}],\"strStoreInfo\":[{\"PKID\":13,\"CustomerID\":1385899,\"Company\":\"山西振东安欣生物制药有限公司\",\"Code\":\"H107285861\",\"NAME\":\"北京祥顺康医药有限公司万顺康诊所\",\"ProductCode\":\"60000004\",\"ProductName\":\"碳酸钙D3颗粒\"}],\"strSectionInfo\":[]},{\"HospitalID\":1385955,\"HospitalCode\":\"H108947538\",\"ExternalID\":\"H108947538\",\"HospitalName\":\"周口市淮阳区人民医院\",\"RegionId\":28,\"Region\":\"河南省\",\"CityId\":\"943\",\"City\":\"周口市\",\"HospitalGradeId\":0,\"HospitalGrade\":null,\"HospitalTypeId\":0,\"HospitalType\":null,\"StopStatusId\":1,\"StopStatus\":\"启用\",\"StatisticsPropertyId\":0,\"StatisticsProperty\":null,\"CountyId\":0,\"County\":null,\"HospitalPropertyId\":0,\"HospitalProperty\":null,\"CustomerAddress\":\"周口市淮阳区106国道与清风路交叉口//周口市淮阳区城关镇民主街2号\",\"HospitalClassificationId\":\"14\",\"HospitalClassification\":\"医院\",\"HospitalGradeUnit\":0,\"DoctorNum\":null,\"CertifiedDoctorNum\":0,\"SpecialistNum\":0,\"SectionOfficeNum\":0,\"BedNum\":null,\"DayOutpatientNum\":0.0,\"YearPurchaseAmount\":0.0,\"ExistingProducts\":null,\"TelePhone\":null,\"ZipCode\":null,\"WebSite\":null,\"LegalPerson\":null,\"Canton\":null,\"IsCountyLevelMedicalId\":0,\"IsCountyLevelMedical\":null,\"ProviderRegisterNO\":\"12411727418746600U\",\"OldName\":\"江苏省淮安市淮阳市第二人民医院,周口市淮阳区第二人民医院,淮阳县公疗医院,淮阳县第二人民医院\",\"Remark\":\"H108947538\",\"ChannelId\":1,\"Channel\":\"生产企业\",\"CreateUserId\":2003610,\"CreateUser\":\"系统管理员03\",\"CreateTime\":\"2023-01-31 17:31:15\",\"UpdateUserId\":2022302,\"UpdateUser\":\"admingww\",\"UpdateTime\":\"2023-09-26 17:13:25\",\"strHospitalMain\":null,\"strDoctor\":[{\"DoctorID\":1062434,\"Doctor\":\"1\",\"DoctorName\":\"1\",\"DoctorCode\":\"1\",\"PhoneNum\":\"1\",\"PositionName\":\"院长\",\"SectionOfficeName\":\"呼吸内科\",\"HospitalPhoneNum\":null,\"SectionOffice\":11,\"POSITION\":10}],\"strStoreInfo\":[{\"PKID\":11,\"CustomerID\":1385955,\"Company\":\"山西振东安欣生物制药有限公司\",\"Code\":\"H109489497\",\"NAME\":\"沈丘县小王楼行政村杨振东卫生室\",\"ProductCode\":\"60000003\",\"ProductName\":\"碳酸钙D₃颗粒\"}],\"strSectionInfo\":[]}]",
        "Msg": "成功"
    }

    取DealResult中的数据存入MongoDB数据库:

    首先,需要解析JSON数据,提取出DealResult字段中的数据,可以使用Python的json模块进行解析。然后,将提取出的数据存入MongoDB数据库,可以使用Python的pymongo模块实现。

    1. from pymongo import MongoClient
    2. import json
    3. # 连接MongoDB数据库
    4. client = MongoClient('localhost', 27017)
    5. db = client['customer_data']
    6. # 创建一个集合(即 MongoDB 中的表)
    7. collection = db['hospital']
    8. def test():
    9. # 解析JSON数据,提取出DealResult字段中的数据
    10. txt = {
    11. "ResultCount": 2,
    12. "Code": "200",
    13. "DealResult": "[{\"HospitalID\":1385899,\"HospitalCode\":\"H108801565 \",\"ExternalID\":\"H108801565\","
    14. "\"HospitalName\":\"浙商银行股份有限公司医务室\",\"RegionId\":23,\"Region\":\"浙江省\",\"CityId\":\"773\","
    15. "\"City\":\"杭州市\",\"HospitalGradeId\":0,\"HospitalGrade\":null,\"HospitalTypeId\":0,"
    16. "\"HospitalType\":null,\"StopStatusId\":1,\"StopStatus\":\"启用\",\"StatisticsPropertyId\":0,"
    17. "\"StatisticsProperty\":null,\"CountyId\":0,\"County\":null,\"HospitalPropertyId\":0,"
    18. "\"HospitalProperty\":null,\"CustomerAddress\":\"杭州市下城区庆春路288号\","
    19. "\"HospitalClassificationId\":null,\"HospitalClassification\":null,\"HospitalGradeUnit\":0,"
    20. "\"DoctorNum\":null,\"CertifiedDoctorNum\":0,\"SpecialistNum\":0,\"SectionOfficeNum\":0,"
    21. "\"BedNum\":null,\"DayOutpatientNum\":0.0,\"YearPurchaseAmount\":0.0,\"ExistingProducts\":null,"
    22. "\"TelePhone\":null,\"ZipCode\":null,\"WebSite\":null,\"LegalPerson\":null,\"Canton\":null,"
    23. "\"IsCountyLevelMedicalId\":0,\"IsCountyLevelMedical\":null,\"ProviderRegisterNO\":null,"
    24. "\"OldName\":null,\"Remark\":\"H108801565\",\"ChannelId\":1,\"Channel\":\"生产企业\","
    25. "\"CreateUserId\":2003610,\"CreateUser\":\"系统管理员03\",\"CreateTime\":\"2022-11-04 16:27:44\","
    26. "\"UpdateUserId\":2022302,\"UpdateUser\":\"admingww\",\"UpdateTime\":\"2023-09-26 17:18:50\","
    27. "\"strHospitalMain\":null,\"strDoctor\":[{\"DoctorID\":1062435,\"Doctor\":\"1\","
    28. "\"DoctorName\":\"1\",\"DoctorCode\":\"1\",\"PhoneNum\":\"1\",\"PositionName\":\"院长\","
    29. "\"SectionOfficeName\":\"老干部科\",\"HospitalPhoneNum\":null,\"SectionOffice\":9,"
    30. "\"POSITION\":10}],\"strStoreInfo\":[{\"PKID\":13,\"CustomerID\":1385899,"
    31. "\"Company\":\"山西振东安欣生物制药有限公司\",\"Code\":\"H107285861\",\"NAME\":\"北京祥顺康医药有限公司万顺康诊所\","
    32. "\"ProductCode\":\"60000004\",\"ProductName\":\"碳酸钙D3颗粒\"}],\"strSectionInfo\":[]},"
    33. "{\"HospitalID\":1385955,\"HospitalCode\":\"H108947538\",\"ExternalID\":\"H108947538\","
    34. "\"HospitalName\":\"周口市淮阳区人民医院\",\"RegionId\":28,\"Region\":\"河南省\",\"CityId\":\"943\","
    35. "\"City\":\"周口市\",\"HospitalGradeId\":0,\"HospitalGrade\":null,\"HospitalTypeId\":0,"
    36. "\"HospitalType\":null,\"StopStatusId\":1,\"StopStatus\":\"启用\",\"StatisticsPropertyId\":0,"
    37. "\"StatisticsProperty\":null,\"CountyId\":0,\"County\":null,\"HospitalPropertyId\":0,"
    38. "\"HospitalProperty\":null,\"CustomerAddress\":\"周口市淮阳区106国道与清风路交叉口//周口市淮阳区城关镇民主街2号\","
    39. "\"HospitalClassificationId\":\"14\",\"HospitalClassification\":\"医院\",\"HospitalGradeUnit\":0,"
    40. "\"DoctorNum\":null,\"CertifiedDoctorNum\":0,\"SpecialistNum\":0,\"SectionOfficeNum\":0,"
    41. "\"BedNum\":null,\"DayOutpatientNum\":0.0,\"YearPurchaseAmount\":0.0,\"ExistingProducts\":null,"
    42. "\"TelePhone\":null,\"ZipCode\":null,\"WebSite\":null,\"LegalPerson\":null,\"Canton\":null,"
    43. "\"IsCountyLevelMedicalId\":0,\"IsCountyLevelMedical\":null,"
    44. "\"ProviderRegisterNO\":\"12411727418746600U\",\"OldName\":\"江苏省淮安市淮阳市第二人民医院,周口市淮阳区第二人民医院,"
    45. "淮阳县公疗医院,淮阳县第二人民医院\",\"Remark\":\"H108947538\",\"ChannelId\":1,\"Channel\":\"生产企业\","
    46. "\"CreateUserId\":2003610,\"CreateUser\":\"系统管理员03\",\"CreateTime\":\"2023-01-31 17:31:15\","
    47. "\"UpdateUserId\":2022302,\"UpdateUser\":\"admingww\",\"UpdateTime\":\"2023-09-26 17:13:25\","
    48. "\"strHospitalMain\":null,\"strDoctor\":[{\"DoctorID\":1062434,\"Doctor\":\"1\","
    49. "\"DoctorName\":\"1\",\"DoctorCode\":\"1\",\"PhoneNum\":\"1\",\"PositionName\":\"院长\","
    50. "\"SectionOfficeName\":\"呼吸内科\",\"HospitalPhoneNum\":null,\"SectionOffice\":11,"
    51. "\"POSITION\":10}],\"strStoreInfo\":[{\"PKID\":11,\"CustomerID\":1385955,"
    52. "\"Company\":\"山西振东安欣生物制药有限公司\",\"Code\":\"H109489497\",\"NAME\":\"沈丘县小王楼行政村杨振东卫生室\","
    53. "\"ProductCode\":\"60000003\",\"ProductName\":\"碳酸钙D₃颗粒\"}], \"strSectionInfo\":[]}]",
    54. "Msg": "成功"
    55. }
    56. result = json.loads(txt['DealResult'])
    57. # 将提取出的数据存入MongoDB数据库
    58. for item in result:
    59. collection.insert_one(item)
    60. return 'ok, 数据存入成功', type(result)
    61. print(test())

     使存入的MongoDB数据库中的数据不重复

    1. from pymongo import MongoClient
    2. import requests
    3. import json
    4. # 连接MongoDB数据库
    5. client = MongoClient('localhost', 27017)
    6. db = client['customer']
    7. # 创建一个集合(即 MongoDB 中的表)
    8. collection = db['hospital_pms']
    9. def test():
    10. url = 'http://172.16.21.3:5013/PMSAPI/MasterData/GetHospitalMainInfo'
    11. data = {
    12. "page": 1,
    13. "pageSize": "10",
    14. "UpdateTimeStart": "2023-09-26",
    15. # "UpdateTimeStart": "2021-01-01",
    16. "UpdateTimeEnd": "2023-09-27"
    17. }
    18. # 传入参数
    19. response = requests.post(url, data=data)
    20. # 解析JSON数据,提取出DealResult字段中的数据
    21. results = json.loads(response.json()['DealResult'])
    22. ids = []
    23. # 将提取出的数据存入MongoDB数据库
    24. for result in results:
    25. result = collection.update_one(
    26. {"HospitalID": result["HospitalID"]}, # 按照HospitalID字段查询是否存在记录
    27. {"$set": result}, # 更新或插入的数据
    28. upsert=True # 插入不存在的记录
    29. )
    30. ids.append(result.upserted_id)
    31. print(result.modified_count, "updated") # 输出更新记录数
    32. print(result.upserted_id, "inserted") # 输出插入记录的ID
    33. return 'ok, 数据存入成功', ids
    34. print(test())

  • 相关阅读:
    stm32f103+HC-SR04+ssd1306实现超声波测距
    530. 二叉搜索树的最小绝对差
    软件测试面试题合集
    数据结构:基于数组实现简单的数据缓存区(简单队列)
    allatori8.0文档翻译-第二步-基础应用混淆
    计算机毕业设计django基于python的热门短视频推荐系统(源码+系统+mysql数据库+Lw文档)
    python3 重启docker方法
    使用未定义的class错误【不完整类型的使用】
    2023年Q3线上生鲜水产数据分析:市场不景气,销额同比下滑44%
    实例分割算法综述
  • 原文地址:https://blog.csdn.net/qq1226546902/article/details/133860205