I tried to export data to csv from Mongodb by using Python. I am getting error: "TypeError: can only join an iterable"
mongodb data sample:
{ "_id" : ObjectId("51dc52fec0d988a9547b5201"),
"hiringManagerIds" : [
"529f5ad1030dedd0a88ed7be",
"529f5ad1030dedd0a88ed7bf"
]
}
Python script:
import codecs
import csv
cursor = db.jobs.find( {}, {'_id': 1, 'hiringManagerIds': 1})
with codecs.open('jobs_array.csv', 'w', encoding ='utf-8') as outfile:
fields = ['_id', 'hiringManagerIds']
write = csv.writer(outfile)
write.writerow(fields)
for x in cursor:
write.writerow((x["_id"], u','.join(x.get('hiringManagerIds'))))
error messages:
Traceback (most recent call last):
File "<stdin>", line 6, in <module>
TypeError: can only join an iterable
I want to remove u letters so I use u.join in the script. the hiringManagerIds filed is missing in some document, so I use get. If I don't add u''.join in the script, it works, but it brings u letters in the csv file. I tried many different ways, but it did not work. Appreciate for any helps. thanks.
print x.get('hiringManagerIds')do?