-
Notifications
You must be signed in to change notification settings - Fork 1
/
msToMongo.py
52 lines (46 loc) · 1.49 KB
/
msToMongo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding:utf-8 -*-
from datetime import datetime
from mongoConnect import mongoConnect
from msSqlConnect import msSqlConnect
from pymongo import MongoClient
from pymongo.errors import ConnectionFailure
import pprint
import pyodbc
import re
# Connect MSSQL
cnxn = msSqlConnect()
# Create a cursor from the connection
cursor = cnxn.cursor()
# Connect MongoDB
client = mongoConnect()
# get Collection
boxes = client.test.boxes
# Delete existing data
boxes.delete_many({})
print('\nMSSQL的資料-----')
try:
# Read the rows one at a time
for row in cursor.execute("SELECT * FROM Boxes"):
print('%-17s %-28s %s' % (row.Name, row.Size, row.Colored))
# Parsing Length, Width, Height
# Compile a pattern to capture float values
length = float(re.findall(r'\d+\.?\d+', row.Size)[0])
width = float(re.findall(r'\d+\.?\d+', row.Size)[1])
height = float(re.findall(r'\d+\.?\d+', row.Size)[2])
print('字串轉換成浮點數: ', length, width, height)
# Insert to MongoDB
box = {"Name": row.Name,
"Length": length,
"Width": width,
"Height": height,
"Colored": row.Colored}
# pprint.pprint(box)
boxes.insert_one(box)
except:
print("MSSQL data not parsed, and didn't insert to MongoDB!!")
print('\nMongoDB的資料-----')
# Read more
for abox in boxes.find({}):
pprint.pprint(abox)
# count
print("總共從MSSQL輸入了", boxes.count_documents({}), "種箱子")