Pythonを使用してMongodbからcsvにデータをエクスポートしようとしました。私はエラーを取得しています: "例外TypeError:だけ反復可能に参加することができます":Pythonを使用してcsvにデータをエクスポートするときにエラーが発生する
{ "_id" : ObjectId("51dc52fec0d988a9547b5201"),
"hiringManagerIds" : [
"529f5ad1030dedd0a88ed7be",
"529f5ad1030dedd0a88ed7bf"
]
}
Pythonスクリプト:
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'))))
エラーメッセージ:
Traceback (most recent call last):
File "<stdin>", line 6, in <module>
TypeError: can only join an iterable
I
MongoDBのデータサンプルを私はスクリプトでu.joinを使用するようにuの文字を削除したい。一部のドキュメントでhiringManagerIdsフィールドが見つからないため、getを使用します。私がスクリプトに参加してもらえればうまくいきますが、csvファイルにu文字が入ります。私はいろいろな方法で試しましたが、うまくいきませんでした。助けを歓迎します。ありがとう。
を '印刷x.get(「hiringManagerIds」)は'何をしますか? – inspectorG4dget
このコードは実行されません。最初のブロックの最後の行のインデントが正しくありませんか? –
それはhiringmanageridsの価値を与えます:[u'529f5ad1030dedd0a88ed7be '、u'529f5ad1030dedd0a88ed7bf'] ありがとう – user7070824