标签:
# -*- coding: utf-8 -*-
import os
import csv
import pymongo
from pymongo import MongoClient
#建立连接
client = MongoClient(‘10.20.4.79‘, 27017)
#client = MongoClient(‘10.20.66.106‘, 27017)
db_name = ‘ta‘ #数据库名
db = client[db_name]
#读取CVS文件并插入到mongoDB数据库 的tvsplst集合中
def InsertDataFromCVS(csvFile):
strFile = unicode(csvFile,‘utf8‘)
if os.path.isfile(strFile):
readFile = file(strFile, ‘rb‘)
reader = csv.reader(readFile)
i = 0
listKey =[]
sqlDict = {}
for line in reader:
i = i + 1
sqlDict.clear()
if i == 1:
for it in line:
if it == ‘PlstID‘:
listKey.append(‘_id‘)
else:
listKey.append(it)
else :
j = 0
for it in line:
sqlDict[listKey[j]] = it.decode(‘gbk‘)
j = j + 1
try:
db.tvsplst.insert(sqlDict)
except:
print i,sqlDict
readFile.close()
#测试代码
strcsvFile=‘D:\\TVSPlst.csv‘
InsertDataFromCVS(strcsvFile)
#从mongoDB数据库中读取tvsplst数据写到CSV文件里面
def WriteTvsPlsttoCsv(csvFile):
strFile = unicode(csvFile,‘utf8‘)
headList =[‘PlstID‘, ‘MediaID‘, ‘PlstDate‘, ‘Hard_STime‘]
writer = csv.writer(file(strFile, ‘wb‘))
writer.writerow(headList)
headList[0]=‘_id‘
for u in db.tvsplst.find():
vList = []
for k in headList:
vList.append(u[k].encode(‘utf8‘))
writer.writerow(vList)
#测试代码
csvfile = ‘D:\\test.csv‘
WriteTvsPlsttoCsv(csvfile)
标签:
原文地址:http://www.cnblogs.com/shaosks/p/5685305.html