From c8b74ef05b8c08d9227a06fc6df427aad20af65e Mon Sep 17 00:00:00 2001 From: Jacek Kowalski Date: Mon, 20 Aug 2018 15:18:42 +0200 Subject: [PATCH] Add python3 support, pretty print table --- .../searchconsole/search_analytics_api_sample.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/samples/searchconsole/search_analytics_api_sample.py b/samples/searchconsole/search_analytics_api_sample.py index 017d47f36f0..de075210c62 100644 --- a/samples/searchconsole/search_analytics_api_sample.py +++ b/samples/searchconsole/search_analytics_api_sample.py @@ -165,22 +165,25 @@ def print_table(response, title): response: The server response to be printed as a table. title: The title of the table. """ - print title + ':' + print(title + ':') if 'rows' not in response: - print 'Empty response' + print('Empty response') return rows = response['rows'] row_format = '{:<20}' + '{:>20}' * 4 - print row_format.format('Keys', 'Clicks', 'Impressions', 'CTR', 'Position') + print(row_format.format('Keys', 'Clicks', 'Impressions', 'CTR', 'Position')) for row in rows: keys = '' # Keys are returned only if one or more dimensions are requested. if 'keys' in row: - keys = u','.join(row['keys']).encode('utf-8') - print row_format.format( - keys, row['clicks'], row['impressions'], row['ctr'], row['position']) + if (sys.version_info > (3, 0)): + keys = ','.join(row['keys']) + else: + keys = u','.join(row['keys']).encode('utf-8') + print(row_format.format( + keys, int(row['clicks']), int(row['impressions']), round(row['ctr'],2), round(row['position'],2))) if __name__ == '__main__': main(sys.argv)