热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

编码实现txt文件导入mongodb,将txt文件中的全文存储到mongodb中

IhavecreatedapythonscriptthatautomatesaworkflowconvertingPDFtotxtfiles.Iwanttobeabletostor

I have created a python script that automates a workflow converting PDF to txt files. I want to be able to store and query these files in MongoDB. Do I need to turn the .txt file into JSON/BSON? Should I be using a program like PyMongo?

I am just not sure what the steps of such a project would be let alone the tools that would help with this.

I've looked at this post: How can one add text files in Mongodb?, which makes me think I need to convert the file to a JSON file, and possibly integrate GridFS?

解决方案

You don't need to JSON/BSON encode it if you're using a driver. If you're using the MongoDB shell, you'd need to worry about it when you pasted the contents.

You'd likely want to use the Python MongoDB driver:

from pymongo import MongoClient

client = MongoClient()

db = client.test_database # use a database called "test_database"

collection = db.files # and inside that DB, a collection called "files"

f = open('test_file_name.txt') # open a file

text = f.read() # read the entire contents, should be UTF-8 text

# build a document to be inserted

text_file_doc = {"file_name": "test_file_name.txt", "contents" : text }

# insert the contents into the "file" collection

collection.insert(text_file_doc)

(Untested code)

If you made sure that the file names are unique, you could set the _id property of the document and retrieve it like:

text_file_doc = collection.find_one({"_id": "test_file_name.txt"})

Or, you could ensure the file_name property as shown above is indexed and do:

text_file_doc = collection.find_one({"file_name": "test_file_name.txt"})

Your other option is to use GridFS, although it's often not recommended for small files.

There's a starter here for Python and GridFS.



推荐阅读
  • Python脚本编写创建输出数据库并添加模型和场数据的方法
    本文介绍了使用Python脚本编写创建输出数据库并添加模型数据和场数据的方法。首先导入相应模块,然后创建输出数据库并添加材料属性、截面、部件实例、分析步和帧、节点和单元等对象。接着向输出数据库中添加场数据和历程数据,本例中只添加了节点位移。最后保存数据库文件并关闭文件。文章还提供了部分代码和Abaqus操作步骤。另外,作者还建立了关于Abaqus的学习交流群,欢迎加入并提问。 ... [详细]
  • 十大经典排序算法动图演示+Python实现
    本文介绍了十大经典排序算法的原理、演示和Python实现。排序算法分为内部排序和外部排序,常见的内部排序算法有插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。文章还解释了时间复杂度和稳定性的概念,并提供了相关的名词解释。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 本文介绍了使用readlink命令获取文件的完整路径的简单方法,并提供了一个示例命令来打印文件的完整路径。共有28种解决方案可供选择。 ... [详细]
  • 如何用Python在MongoDB中导入JSON文件? ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 用Python在Windows上安装MongoDB原文 ... [详细]
  • Python爬虫使用MongoDB 提示No connection adapters were found for如何解决? ... [详细]
  • 背景最近项目中用到了mongodb,并且用python的pymongo包操作。本文就把目前遇到的问题和学习经历做个小结,方便日后查询。Mongodb启动安装mongodbhttp ... [详细]
  • 大部分情况下爬取的数据特别灵活,不一定只有指定的几个字段数据,这时候就需要将数据存储在非关系型数据库中了,MongoDB是由C语言编写的& ... [详细]
  • 如何用Python为MongoDBCollection创建索引? ... [详细]
  • 本文介绍了在Win10上安装WinPythonHadoop的详细步骤,包括安装Python环境、安装JDK8、安装pyspark、安装Hadoop和Spark、设置环境变量、下载winutils.exe等。同时提醒注意Hadoop版本与pyspark版本的一致性,并建议重启电脑以确保安装成功。 ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • 本文介绍了如何使用python从列表中删除所有的零,并将结果以列表形式输出,同时提供了示例格式。 ... [详细]
author-avatar
涂凌萱_TLX_9s7_140
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有