0

I am hoping to get some help with Parsing data retrieved from the Google DataStore Client in Python.

I am required to create a process in which I have to parse some data taken from the datastore. I am currently calling to retrieve data via this method: https://cloud.google.com/datastore/docs/concepts/entities#retrieving_an_entity

If I am to print what is returned I get the below:

<Entity(u'Example', u'1000') {u'some_data': True, u'some_more_data': False}>

If I could be shown an example on how to best parse the information returned in the Dict I would be very grateful, so that I can take each property and its value in a For Each Loop. I.e:

'some_data': True

Thanks for your time, Jordan

2 Answers 2

1

In Python, properties can be accessed just like object attributes.

For example, after you retrieve your entity:

key = client.key("yourkey")
example = client.get(key)

You can access its properties by their name and use them

print "'some_data': " + example.some_data
print "'some_more_data': " + example.some_more_data

To get a list of an entity's properties, use the instance_properties() method:

for property in example.instance_properties():
    value = getattr(example, property)

Read more: https://www.safaribooksonline.com/library/view/programming-google-app/9780596157517/ch04s06.html

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

1 Comment

Thanks for this Neri, I tried this unfortunately it did not solve my problem. However, I have found a working solution I have posted as a Answer to the question!
0

I managed to figure out a working solution.

from google.cloud import datastore

datastore_client = datastore.Client()

def parse_example_list():
    kind = 'Kind'
    name = 'Name'
    key = datastore_client.key(kind, name)
    returned_entity = datastore_client.get(key)

    property_list = {}

    for p in returned_entity.items():
        property_list[p[0]] = p[1]

    return property_list

This returns:

{u'my_first_property': u'My Propertys Value'}

Hope this helps!

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.