UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 16383: unexpected end of data

491 views
Skip to first unread message
Message has been deleted

christos...@fromatob.com

unread,
May 4, 2018, 6:20:43 AM5/4/18
to AdWords API and Google Ads API Forum
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

Dhanya Sundararaju (AdWords API Team)

unread,
May 4, 2018, 12:00:04 PM5/4/18
to adwor...@googlegroups.com
Hi Christos,

Thank you for the details. I was able to printout the contents of String buffer as below:

1042246504,51871592140,AdName_äüö,246349695969,0,0,0

Below is the code snippet I had used:
report = {
 
'reportName': 'Last 7 days AD_PERFORMANCE_REPORT',
 
'dateRangeType': 'LAST_7_DAYS',
 
'reportType': 'AD_PERFORMANCE_REPORT',
 
'downloadFormat': 'CSV',
 
'selector': {
 
'fields': ['CampaignId', 'AdGroupId', 'AdGroupName', 'Id', 'Impressions', 'Clicks', 'Cost']
 
}
}

report_data
= StringIO.StringIO()
report_downloader
.DownloadReport(report, report_data, skip_report_header=False, skip_column_header=False,
 skip_report_summary
=False, include_zero_impressions=True)

print report_data.getvalue()
report_data
.close()

Could you please reply back with your client customer id and report definition so that I can troubleshoot further? Also, could you please point me to specific examples? You may opt to reply privately to author.


Regards,
Dhanya, AdWords API Team

Thanet Knack Praneenararat (AdWords API Team)

unread,
May 9, 2018, 12:03:54 AM5/9/18
to AdWords API and Google Ads API Forum
Hi Christos,

We can't reproduce your issue on our end, using both Python 2 and 3, so it doesn't look like this is an issue on AdWords API server.
I'll ask the Python library owner to continue following up on GitHub.

Best,
Thanet, AdWords API Team
Reply all
Reply to author
Forward
0 new messages