中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

python實(shí)現(xiàn)將excel數(shù)據(jù)添加到mongodb

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
在做文本處理時(shí),有一些數(shù)據(jù)存儲(chǔ)到了excel中,為了將數(shù)據(jù)導(dǎo)入mongodb,引入了pymongo,xlrd包

利用pymongo包進(jìn)行數(shù)據(jù)庫的連接

使用xlrd包讀取excel數(shù)據(jù),由于二者數(shù)據(jù)結(jié)構(gòu)的不同,要將excel格式數(shù)據(jù)轉(zhuǎn)換為json格式數(shù)據(jù)

由于編碼問題會(huì)出現(xiàn)“TypeError: 'str' object does not support item assignment

要利用json.loads方法對(duì)數(shù)據(jù)進(jìn)行解碼

#coding=utf-8
 
import xlrd
import sys
import json
import pymongo
from pymongo import MongoClient
 
#連接數(shù)據(jù)庫
client=MongoClient('localhost',27017)
db=client.scrapy
account=db.weibo
 
data=xlrd.open_workbook('test.xlsx')
table=data.sheets()[0]
#讀取excel第一行數(shù)據(jù)作為存入mongodb的字段名
rowstag=table.row_values(0)
nrows=table.nrows
#ncols=table.ncols
#print rows
returnData={}
for i in range(1,nrows):
    #將字段名和excel數(shù)據(jù)存儲(chǔ)為字典形式,并轉(zhuǎn)換為json格式
    returnData[i]=json.dumps(dict(zip(rowstag,table.row_values(i))))
    #通過編解碼還原數(shù)據(jù)
    returnData[i]=json.loads(returnData[i])
    #print returnData[i]
    account.insert(returnData[i])

標(biāo)簽: 數(shù)據(jù)庫

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:Android 聽筒播放模式實(shí)現(xiàn)代碼

下一篇:PHP簡(jiǎn)單的圖片驗(yàn)證碼實(shí)現(xiàn)