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模块实现。

from pymongo import MongoClient
import json

# 连接MongoDB数据库
client = MongoClient('localhost', 27017)
db = client['customer_data']
# 创建一个集合(即 MongoDB 中的表)
collection = db['hospital']


def test():
    # 解析JSON数据,提取出DealResult字段中的数据
    txt = {
        "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": "成功"
    }
result = json.loads(txt['DealResult'])
    # 将提取出的数据存入MongoDB数据库
    for item in result:
        collection.insert_one(item)
    return 'ok, 数据存入成功', type(result)


print(test())

 

你可能感兴趣的:(Python,MongoDB,python,mongodb,数据库)