栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

# Python脚本读取CSV文件写入MongoDB数据库中 #mongoDB for window # 可视化工具

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

# Python脚本读取CSV文件写入MongoDB数据库中 #mongoDB for window # 可视化工具

1.第一种上传方法
#coding:utf8
from pymongo import MongoClient
import csv
import time
def connect_mongo():
    # mongo_uri = 'mongodb://user_name:password@host:port/Validate the database'
    # client = pymongo.MongoClient(mongo_uri, readPreference='write as required')
    client = MongoClient('127.0.0.1', 27017)
    db = client.test
    collection = db['second_crimes']
    return collection
    #集合
def insertToMongoDB(set1):
    with open('second_crimes.csv','r',encoding='utf-8') as csvfile:
        # Call the DictReader function in csv to directly obtain the data in the form of a dictionary
        reader = csv.DictReader(csvfile)
        csv_data = []
        # Create a counts to count how many pieces of data have been added
        counts = 0
        index = 1
        for each in reader:
            csv_data.append(each)
            if index==10000:#Write to MongoDB after 10,000
                set1.insert(csv_data)
                csv_data.clear()
                index = 0
                print("successfully added" + str(counts) + "data")
            counts+=1
            index+=1
        if len(csv_data)>0:#remain data
            set1.insert(csv_data)
            print("Successfully added %s data"%len(csv_data))
if __name__=='__main__':
    print(time.strftime('%Y-%m-%d %H:%M:%S'))#for calculating time
    set1 = connect_mongo()
    insertToMongoDB(set1)
    print(time.strftime('%Y-%m-%d %H:%M:%S'))
2.第二种方式
# coding=utf-8
from pymongo import MongoClient
import csv, time, re
# 链接mongodb数据库客户端
client = MongoClient('127.0.0.1', 27017)
# 链接mongodb数据库集合
crimes = client['test']['crimes']
# 将所有数据全部插入到对应集合中
# crimes集合
_data = csv.reader(open('crimes.csv', 'r'))
count = 0
for _ in list(_data)[1:]:
    _json = {
        'id': _[0],
        'case_number': _[1],
        'data': _[2],
        'block': _[3],
        'iucr': _[4],
        'primary_type': _[5],
        'description': _[6],
        'location_description': _[7],
        'arrest': _[8],
        'domestic': _[9],
        'beat': _[10],
        'district': _[11],
        'word': _[12],
        'connuntiy_area': _[13],
        'fbi_code': _[14],
        'x_coordinate': _[15],
        'y_coordinate': _[16],
        'year': _[17],
        'updated_on': _[18],
        'latitude': _[19],
        'longitude': _[20],
        'location': _[21]
    }
    crimes.insert_one(_json)
    count += 1
    print('第' + str(count) + '个数据已经插入成功!' + ' 数据源是:' + str(_json))
3.MongoDB 可视化工具链接 点击直接下载: nosqlbooster4mongo-7.0.8.exe. 4.MongoDB window系统安装软件 点击直接下载: mongodb-windows-x86_64-5.0.5.zip.
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/715398.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号