Hi Ben, thanks for replying.
This is what worked for me, and didn't break so far, after weeks importing
data for several clients:
from google.protobuf.json_format import MessageToJson
# ...
response = service.search_stream(customer_id, query=query)
for batch in response:
for row in batch.results:
serialized_json = MessageToJson(row).replace('\n', '')
snakefied = re.sub(r'\"([a-zA-Z0-9_]+?)\"\:\s', self.camel2snake,
serialized_json)
values = json.loads(snakefied)
# ...
Cheers,
Deny
On Monday, October 26, 2020 at 12:19:43 PM UTC-3 adsapiforumadvisor wrote:
> Hi Deny,
>
> Just to clarify your issue, are you hoping to iterate over the fields of,
> for instance, a GoogleAdsRow
> <https://github.com/googleapis/googleapis/blob/master/google/ads/googleads/v5/services/google_ads_service.proto#L356>
>
> object, which can have varied fields depending on the query you sent, (i.e.
> row.campaign, row.metrics, etc)? If so I can see why it's confusing,
> because those objects aren't like dicts, where you can simply use a for
> loop to easily access each field.
>
> For background, these objects are protobuf message instances
> <https://developers.google.com/protocol-buffers/docs/pythontutorial> and
> behave quite differently from other traditional Python objects. I admit
> that the interface these objects present is definitely confusing and not
> Pythonic at all, and in fact we're working to make the surface more
> sensible and hope to produce a number of improvements in the future.
>
> In the meantime, it's possible to know from the request object which
> fields are present on each GoogleAdsRow instance, here's an example using
> search_stream:
>
>
>
> import functools
>
> # Borrowed from:
> https://github.com/googleads/google-ads-python/blob/master/google/ads/google_ads/util.py#L51
> def get_nested_attr(obj, attr, *args):
> def _getattr(obj, attr):
> return getattr(obj, attr, *args)
>
> return functools.reduce(_getattr, [obj] + attr.split("."))
>
> for batch in response:
> fields = batch.field_mask.paths
> for row in batch.results:
> for field in fields:
> # field will be a str path, i.e. "campaign.id" or
> "metrics.average_cost"
> value = get_nested_attr(row, field)
> # you can also define logic based on the field name
> if "campaign.id" in field:
> # do something
>
>
> That's not particularly elegant, but it might be a bit faster than your
> solution that requires the entire object to be serialized.
>
> Like I mentioned, making this interface simpler is a big priority for us.
> If you have any other issues/questions/feature requests related to this it
> may be better to post directly on our Github Issues Tracker
> <https://github.com/googleads/google-ads-python/issues>. In Github you
> can also keep track of new releases/changes and hopefully we'll be able to
> release some improvements there soon.
>
> Best,
>
> Ben Karl, Google Ads API Team
>
>
> ref:_00D1U1174p._5004Q25Wv5e:ref
>
--
--
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog:
https://googleadsdeveloper.blogspot.com/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
You received this message because you are subscribed to the Google
Groups "AdWords API and Google Ads API Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en
---
You received this message because you are subscribed to the Google Groups
"AdWords API and Google Ads API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/adwords-api/94b4fd22-4921-483b-a7bf-40297c7bce93n%40googlegroups.com.