# 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.

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大白菜程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值