Python joins MongoDB to extract data from some fields and write to TXT file

Source: Internet
Author: User
Tags joins mongoclient

Department is responsible for the industrial domain knowledge map construction, for industrialization and information fusion, sounds good tall and difficult, anyway, I can not understand so deep, fortunately, the department manager led.

Want to do professional domain knowledge map First of all have to have the domain knowledge, where does this knowledge come from? The main source is definitely crawling from the internet!

Climb down a lot of knowledge directly piled up in that certainly not, but also to establish a relationship, one of the important is the text classification! So how do you classify it?

Let's talk about it later ... Because whatever you do, start by taking out the data you want in the database.

#Coding=utf-8Importsysreload (SYS) sys.setdefaultencoding ('Utf-8') fromPymongoImportmongoclient#establishing a MongoDB database connectionClient = Mongoclient ('IP', 27017)#user authenticationdb =client. Library name Db.authenticate ("Account Number","Password")#The collection used to connect, which is what we usually call a tablecollection=db. Table name#Then you can use collection to do some of the database tables.With open ('file name. txt','WB') as F:
#接下来可实现提取想要的字段内的数据 forIteminchCollection.find ({}, {"Summary": 1,"Manual": 1,"Claim": 1,"_id": 0}):ifItem.has_key ('Summary') anditem['Summary']: f.write (item['Summary']) ifItem.has_key ('Manual') anditem['Manual']: f.write (item['Manual']) ifItem.has_key ('Claim') anditem['Claim']: f.write (item['Claim']) F.write ('\ n')

Python joins MongoDB to extract data from some fields and write to TXT file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.