Python串連mongodb提取部分欄位內資料並寫入txt檔案

來源:互聯網
上載者:User

標籤:專業   mic   sys   write   txt   font   bsp   python   文本   

      部門負責工業領域知識圖譜構建,用於工業化與資訊化兩化融合,聽起來好高大上好難,反正目前的我還理解不了那麼深,好在有部門經理帶領。

      想要做專業領域知識圖譜首先要有專業領域知識,這些知識從哪來呢?主要來源肯定是從互連網上爬啦!

      爬下來一大堆知識直接堆在那肯定不行,還要建立起相互關係,其中重要一項就是文本分類啦!那麼怎麼分類呢?

      還是以後再說吧。。。因為不管怎麼做,首先都要把資料庫裡自己想要的資料取出來。

#coding=utf-8import sysreload(sys)sys.setdefaultencoding(‘utf-8‘)from pymongo import MongoClient#建立MongoDB資料庫連接client = MongoClient(‘IP‘, 27017)#使用者驗證db = client.庫名db.authenticate("帳號", "密碼")#串連所用集合,也就是我們通常所說的表collection=db.表名#接下裡就可以用collection來完成對資料庫表的一些操作with open (‘檔案名稱.txt‘, ‘wb‘) as f:
#接下來可實現提取想要的欄位內的資料 for item in collection.find({}, {"Summary":1,"Manual":1,"Claim":1,"_id":0}): if item.has_key(‘Summary‘) and item[‘Summary‘]: f.write(item[‘Summary‘]) if item.has_key(‘Manual‘) and item[‘Manual‘]: f.write(item[‘Manual‘]) if item.has_key(‘Claim‘) and item[‘Claim‘]: f.write(item[‘Claim‘]) f.write(‘\n‘)

 

Python串連mongodb提取部分欄位內資料並寫入txt檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.