0

I am getting the error:

Traceback (most recent call last):
  File "<stdin>", line 9, in <module>
AttributeError: 'NoneType' object has no attribute 'get'

Is there something wrong with the following script? I am using get() in the script because of some phone field names missing in Mongodb collections.

The sample data:

{
    "_id": ObjectID("57e99f88b948da6c3be04366"),
    "email": "[email protected]",
    "phone": [
        {
            "type": "Mobile",
            "number": "250-851-1041"
        }
    ]}

the python script :

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)

please someone can help me.

5
  • Run it through a debugger or print things. For starters, print(x) just inside the for loop. Commented Nov 2, 2016 at 21:22
  • The error means that x is None, which means cursor contains None. Figure out why that is happening. Commented Nov 2, 2016 at 21:33
  • I checked data in the collection that there are not phone field name in some rows. that is why I use get() to skip them. thanks Commented Nov 2, 2016 at 21:38
  • It's not the field that's missing, it's the record itself. Commented Nov 2, 2016 at 21:41
  • do you have any idea to fix the problem? thanks Commented Nov 2, 2016 at 22:29

2 Answers 2

1

Are you connected to the correct database, the one that contains the users collection and not just the default database?

Sign up to request clarification or add additional context in comments.

3 Comments

yes, I connected to the correct database by using pymongo.
ubuntuforums.org/showthread.php?t=2262024 follow this link it may help you not sure,just give a try
it worked after changed y = x.get('phone') to for y in x.get('phone', {}): but I got the repeated rows. Can someone know how to fix it?
0

I worked out the problem. I just share it with you. here is the solution. thanks you for help.

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import codecs
import csv
cursor = db.users.find ({}, {'_id':1, 'phone.type':1, 'phone.number':1, 'phone.extension':1})
with codecs.open('phone.csv', 'w', encoding='utf-8') as outfile:        
        fields =['_id', 'phone.type', 'phone.number', 'phone.extension'] 
        write = csv.DictWriter(outfile, fieldnames=fields)
        write.writeheader()    
        for x in cursor:
            x_id = x['_id']                                        
            for y in x.get('phone', {}):                                                                        
                z = {'_id': x_id,                                    
                    'phone.type': y.get('type'),
                    'phone.number': y.get('number'),
                    'phone.extension': y.get('extension')}
                print z                                                                                              
                write.writerow(z) 

cursor.close()  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.