【python】遍历一个目录下的所有CSV并插入MONGODB

上一篇讲到把股票的历史数据下载成了CSV,查看虽然方便,但还是希望在数据库中存一份,方便存储和操作。

对新建的表还是和之前一样在mongodb compass里面插入一条记录,并建立索引
在这里插入图片描述
话不多说直接上代码:

'''
this file is used to import all scv under a directory into mongodb
'''
# encoding:latin1
import os
import pymongo
import csv
from pymongo import MongoClient
from pymongo import errors
import codecs


def str_to_hex(s):
    return ' '.join([hex(ord(c)).replace('0x', '') for c in s])


def analysis_file(file):
    csv_file = csv.reader(file)   # 将读入的文件转换成csv.reader对象
    head_row = next(csv_file)   #去除第一行标题

    for row in csv_file:
        history_item = dict()
        history_item["date"] = row[0]
        history_item["stock_id"] = row[1]
        history_item["stock_name"] = row[2]
        history_item["closing_price"] = row[3]
        history_item["highest_price"] = row[4]
        history_item["lowest_price"] = row[5]
        history_item["open_price"] = row[6]
        history_item["last_closing_price"] = row[7]
        history_item["rise_amount"] = row[8]
        history_item["rise_range"] = row[9]
        history_item["turn_over_rate"] = row[10]
        history_item["turn_over_hand"] = row[11]
        history_item["turn_over_amount"] = row[12]
        history_item["market_value"] = row[13]
        history_item["circulation_market_value"] = row[14]
        # print(history_item)
        try:
            collection.insert(history_item)
        except errors.DuplicateKeyError:
            pass


def read_file_list(file_path):
    file_list = os.listdir(file_path)  #读取路径下所有文件名
    for file in file_list:
        if file[-3:] != 'csv':  # 检查是不是csv文件
            continue
        path = os.path.join(file_path, file)  # 通过文件夹路径和文件名生成文件绝对路径
        print(path)
        f = codecs.open(path, 'rb', 'gbk')  # 使用适合的编码打开文件,不知道的话就多试几下
        analysis_file(f)


if __name__ == '__main__':
    client = MongoClient('127.0.0.1:27017')  #连接mongodb
    db = client['stock']  #选择库
    collection = db['stock_history']  # 选择表
    read_file_list('/stock_history/stock_history/history_data')  # 路径自定义

运行后就可以看到结果了:
在这里插入图片描述
数据已经顺利导入很简单吧~

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值