1. 程式人生 > >python讀取cvs檔案並寫入mongodb

python讀取cvs檔案並寫入mongodb

1環境

mongodb3.4.7

python2.7.5(centos7自帶版本)

pymongo-2.7

pydata.csv(自己的資料檔案)

2.dataTrans.py

# -*- coding:utf-8 -*-

import pymongo
import csv
import datetime

#build connection
conn = pymongo.MongoClient('192.168.1.6',20000)
#connect dataBase
mydb = conn.test
#get collection
myCollect = mydb.testCollect
#get data
csvfile = file('/mongodbData/pydata.csv','rb')
#csv_reader = csv.reader(csvfile)
csv_reader = csv.DictReader(csvfile)
#get time befor writing
print datetime.datetime.now()
for i in range(3,10003):
    for row in csv_reader:
        #print data, for test
        #print row    
        #print(row['deviceId'])
        row['deviceId'] = int(row['deviceId'])
        #row['deviceId'] = row['deviceId']+ i
        #print row
        myCollect.insert(row)
#get time after writing
print datetime.datetime.now()
csvfile.close()

3.查詢結果


相關推薦

python讀取cvs檔案寫入mongodb

1環境 mongodb3.4.7 python2.7.5(centos7自帶版本) pymongo-2.7 pydata.csv(自己的資料檔案) 2.dataTrans.py # -*- codi

python 讀取excel檔案寫入json

excel內容:程式碼: import xlrd import json import operator def read_xlsx(filename): # 開啟excel檔案 data1 = xlrd.open_workbook(file

一個python讀取cvs檔案處理畫圖的程式

import csv from matplotlib import pyplot as plt from datetime import datetime #讀取CSV檔案資料 filename='sitka_weather_2014.csv' with

Python讀取Excel檔案生成分析結果

筆者需要對存放在Excel裡的很多媒資檔案進行分析,這些節目的解析度有高清、標清之分,高清的節目名稱前面加上“HD-”,比如下面的“HD-護寶聯盟第一季”,標清的則直接是節目名稱,如“HD-護寶聯盟第一季”,這些節目可能分別屬於電視劇、電影、娛樂的欄目,每個節目有對應的時長(分鐘數),然後按欄目放在不同的sh

python3 迴圈讀取excel檔案寫入json

檔案內容: excel內容: 程式碼: import xlrd import json import operator def read_xlsx(filename): # 開啟excel檔案 data1 = xlrd.open_workbo

python讀取excel檔案匯入mysql資料庫

import MySQLdb import xlrd import xlwt from datetime import date,datetime   database = MySQLdb.connect (host="114.116.2.203", user = "r

python 讀取excel表格寫入sqllite資料庫

近日想把excel的電影目錄,寫入sqllite資料庫 方便以後查詢,新增等 用到了xlrd 下面是第一部分 建立資料庫表 #1080P目錄.xls excel檔案 #_*_ coding:utf-8 _*_ import os,sys,sqlite3,string

Win7,64位下Python讀取Excel檔案繪製圖表

1、安裝xlrd的whl檔案:     Python讀取Excel使用xlrd,到官網https://pypi.python.org/pypi/xlrd下載xlrd-1.0.0-py3-none-any.whl安裝。     在C:\Program Files\Python

python 讀取txt檔案儲存為array

Reading Text Tables with Python Reading tables is a pretty common thing to do and there are a number of ways to read tables besides writi

Spring batch教程 之 讀取CSV檔案寫入MySQL資料庫

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-inst

使用python讀取.txt檔案儲存到Excel中

txt檔案中使用json格式儲存資料,使用Excel中的Workbook函式可以實現行中單元格的值輸入 worksheet.cell(r=i, c=j).value = file_cintent[s

python讀取json檔案將資料插入到mongodb

#coding=utf-8 import sunburnt import urllib from pymongo import Connection from bson.objectid import ObjectId import logging from dateti

U盤大盜,Python檢測U盤插入、自動複製檔案寫入檔案

  技術要點:                 學習Python中有不明白推薦加入交流群               &

Java之建立目錄、檔案寫入讀取檔案內容

1.mkdirs():可以建立多級資料夾, mkdir()只會建立一級的資料夾。 2.getParentFile():獲得父目錄。 3.file.getParentFile().exists():判斷父

Python解析csv檔案將結果寫入bat檔案

            第二次感覺有很大進步,對列表使用慢慢熟悉,繼續加油。 #!/usr/bin/python # -*- coding: UTF-8 -*- import os import csv import codecs

python檢測U盤的插入,以及進行自動複製檔案寫入檔案

技術要點分析: 1.如何檢測有U盤的插入。 2.如何複製U盤裡面的東西 3.如果U盤可寫,如何寫入檔案到U盤裡面。 # -*- coding: utf-8 -*- # @Time : 20

python建立檔案寫入內容

#建立檔案,msg即要寫入的內容 def create__report(msg): report_path=get_aft_path()+"\\Report\\report.html" f=open(pass_report_path,"a") f.write("<met

Win7,64位下Python 讀取csv檔案(Excel轉化的)繪製圖表

參考程式碼: 依賴的包: 1.matplot 2.numpy Python 讀取csv檔案(Excel轉化的)並繪製圖表。 程式碼如下: from __future__ import print_function from matplotlib import mla

python 在excel檔案寫入date日期資料,以及讀取excel日期資料,如何在python中正確顯示date日期。

如何通過python寫入date資料了? 寫入還是很簡單的。 import xlwt3 import  datetime as dt workbook = xlwt.Workbook() worksheet = workbook.add_sheet('Sheet1') wo

python讀取.txt檔案資料和將資料寫入檔案.txt

一.讀取.txt檔案中的資料到張量中 說明:將data_x.txt和data_y.txt中的資料分別讀取到x_data張量中 #!/usr/bin/python # coding=utf-8 im