Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add python3 support, pretty print table #560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions samples/searchconsole/search_analytics_api_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)