に私はエラーをエラーNonetypeを取得していますガイド:は、Python 2.7
Traceback (most recent call last):
File "<stdin>", line 9, in <module>
AttributeError: 'NoneType' object has no attribute 'get'
は、次のスクリプトに何か問題はありますか?私はMongodbコレクションに欠けているいくつかの電話フィールド名のためにスクリプトでget()を使用しています。
サンプルデータ:
{
"_id": ObjectID("57e99f88b948da6c3be04366"),
"email": "[email protected]",
"phone": [
{
"type": "Mobile",
"number": "250-851-1041"
}
]}
Pythonスクリプト:
import codecs
import csv
cursor = db.users.find ({}, {'_id':1, 'email': 1, 'phone.type':1, 'phone.number':1})
with codecs.open('applicatonmethod3.csv', 'w', encoding='utf-8') as outfile:
fields = ['_id', 'email', 'phone.type', 'phone.number']
write = csv.DictWriter(outfile, fieldnames=fields)
write.writeheader()
for x in cursor:
x_id = x.get('_id')
x_email = x['email']
y = x.get('phone')
z = {'_id': x_id,
'email': x_email,
'phone.type': y.get('type'),
'phone.number': y.get('number')}
write.writerow(z)
誰かが私を助けることができますしてください。
デバッガで実行するか、印刷します。まず、forループの内側で 'print(x)'を実行します。 –
エラーは 'x'が' None'であることを意味します。これは 'cursor'が' None'を含んでいることを意味します。なぜそれが起こっているのか理解してください。 – kindall
コレクション内のデータで、一部の行に電話フィールド名がないことを確認しました。そのため、私はget()を使ってそれらをスキップします。ありがとう – user7070824