使用 Python 将 txt 内容导入到 MongoDB

创建
阅读 556

安装依赖

pip3 install -i https://mirrors.aliyun.com/pypi/simple/ mongoengine

逐行读取文件并导入

import mongoengine
import json

MONGODB_HOST = "192.168.1.2"
MONGODB_PORT = 27017
MONGODB_DB = "users"
# MONGODB_USERNAME = "xxxxxx"
# MONGODB_PASSWORD = "xxxxxxx"

conn = mongoengine.connect(
    db=MONGODB_DB,
    host=MONGODB_HOST,
    port=MONGODB_PORT,
    # username = MONGODB_USERNAME,
    # password = MONGODB_PASSWORD
)

db = conn[MONGODB_DB]
# db.authenticate(MONGODB_USERNAME,MONGODB_PASSWORD)

dbCollection = db.user

with open('user.txt', 'r', encoding='utf-8') as f:
    while True:
        #  逐行读取
        line = f.readline()
        if not line:
          print('读取完成')
          break
        items = line.strip('\r').strip('\n').split('----')
        dbCollection.insert_one({'name': items[0], 'phone': items[1]})

如果数据数据量大,批量读取,提高效率,但是更占内存

import mongoengine
import json

MONGODB_HOST = "192.168.1.2"
MONGODB_PORT = 27017
MONGODB_DB = "users"
# MONGODB_USERNAME = "xxxxxx"
# MONGODB_PASSWORD = "xxxxxxx"

conn = mongoengine.connect(
    db=MONGODB_DB,
    host=MONGODB_HOST,
    port=MONGODB_PORT,
    # username = MONGODB_USERNAME,
    # password = MONGODB_PASSWORD
)

db = conn[MONGODB_DB]
# db.authenticate(MONGODB_USERNAME,MONGODB_PASSWORD)

dbCollection = db.user

with open('user.txt', 'r', encoding='utf-8') as f:
  while True:
    lines = f.readlines(1000000)
    items = []
    if not lines:
        print('读取完成')
        break

    for line in lines:
        item = line.strip('\r').strip('\n').split('----')

        items.append({'name': item[0], 'phone': item[1]})
    dbCollection.insert_many(items)

本文链接 https://www.yidiankuaile.com/post/import-from-txt-to-mongodb-throght-python

最后更新