HI,
I have already created an
issue in github and was advised to ask here for help.
I am getting the error:
Traceback (most recent call last):
File "test.py", line 267, in <module>
client_list.append(tostring(adwords_client, report_name, id))
File "test.py", line 243, in tostring
skip_report_summary=True, include_zero_impressions=True)
File ".../anaconda3/lib/python3.6/site-packages/googleads/common.py", line 531, in Wrapper
return utility_method(*args, **kwargs)
File ".../anaconda3/lib/python3.6/site-packages/googleads/adwords.py", line 1292, in DownloadReport
output, **kwargs)
File ".../anaconda3/lib/python3.6/site-packages/googleads/adwords.py", line 1569, in _DownloadReport
and not isinstance(output, io.BytesIO))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 16383: unexpected end of data
while trying to download an Ad Perfromance report as string in memory with the DownloadReport function or as stream with DownloadReportAsStream function.
The problem comes from the AdGroupName field that contains german characters like ä, ü, ö.
I am not receiving any error though when I use the DownloadReportAsStream function to write the report in a .csv file or when I force the decoding to be 'latin-1' with
report_data.write(chunk.decode('latin-1') if sys.version_info[0] == 3
and getattr(report_data, 'mode', 'w') == 'w' else chunk)
I am using Python 3, API v201802 and the locale in the container is set to be C.UTF-8.
Below are the functions which are mostly copied from AdWords API documentation.
write in csv (working):
def write_csv(client, report, customer_id):
client.SetClientCustomerId(customer_id)
report_downloader = client.GetReportDownloader(version='v201802')
filename = str(customer_id) + '_' + report + '_' + min_date
report_data = io.open(filename, 'wb')
stream_data = report_downloader.DownloadReportAsStream(
reports[report], skip_report_header=True, skip_column_header=False,
skip_report_summary=True, include_zero_impressions=True)
try:
while True:
chunk = stream_data.read(CHUNK_SIZE)
if not chunk:
break
report_data.write(chunk.decode() if sys.version_info[0] == 3
and getattr(report_data, 'mode', 'w') == 'w' else chunk)
finally:
report_data.close()
stream_data.close()
stream with latin-1 (working):
def stream(client, report, customer_id):
client.SetClientCustomerId(customer_id)
report_downloader = client.GetReportDownloader(version='v201802')
report_data = io.StringIO()
stream_data = report_downloader.DownloadReportAsStream(
reports[report], skip_report_header=False, skip_column_header=False,
skip_report_summary=False, include_zero_impressions=True)
try:
while True:
chunk = stream_data.read(CHUNK_SIZE)
if not chunk:
break
report_data.write(chunk.decode('latin-1') if sys.version_info[0] == 3
and getattr(report_data, 'mode', 'w') == 'w' else chunk)
return report_data
finally:
report_data.close()
stream_data.close()
write to string (not working):
def tostring(client, report, customer_id):
client.SetClientCustomerId(customer_id)
report_downloader = client.GetReportDownloader(version='v201802')
report_data = io.StringIO()
report_downloader.DownloadReport(
reports[report], report_data, skip_report_header=True, skip_column_header=False,
skip_report_summary=True, include_zero_impressions=True)
return report_data