0

retrieve.py

import pymongo
from pymongo import MongoClient
import gridfs
connection = MongoClient('localhost', 27017)
db = connection['file_uploaded_new']
data = db['words.chunks']

retrieve = data.find()

for item in retrieve:
    a=(str(item["data"]))
    f=open(a,"rb")
    line = f.readline()
    print(line)
    f.close()

Using this code gives me error:: Invalid argument: 'b\'PK\x03....'

MOngoDB data:: data inside words.chunk

I want to read the binary data in text format. How to approach it as above code only gives me binary data.I want to read binary file stored in data mongoDB in binary format using python script. P.S.: I uploaded into mongoDB using gridfs in node js.

Ajax
  • 99
  • 5
  • 13
  • Change `data = db['words.chunk']` to `data = db['words.chunks']` missing plural form – Subburaj Jan 23 '20 at 05:49
  • Oops. Thank you very much it worked. Now the data is in binary. How can I read the data in text format? – Ajax Jan 23 '20 at 05:56

1 Answers1

0

You are missing plural form so change

data = db['words.chunk']

TO

data = db['words.chunks']

For How can I read the data in text format? I am not a python background developer so I can't give you clear code to do that, the following link may be solve your problem

Binary to String/Text in Python

Subburaj
  • 5,114
  • 10
  • 44
  • 87