Forum Archive

Related with Non-ASCII error

WayneJang

I took a Coursera Python lecture by Dr. Chuck, and I would like to run his code on my Pythonista - but it is not working, which works perferctly on my laptop. The special feature of this code is to use Google API - and I got 'SystemError:invalid syntax' without any further explanation. My code is below, and I really appreciate any advice on this matter in advance. Also, thanks for dgelessus with helpful comment that I am able to use 'code block' that enables me to upload the post with the indentation.


import urllib
import json

serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'

while True:
    address = raw_input('Enter location : ')
    if len(address) < 1: break
    url = serviceurl + urllib.urlencode({'sensor':'false','address':address})
    print 'Retrieving ', url
    uh = urllib.urlopen(url)
    data = uh.read()
    print 'Retrieved ', len(data), 'characters'
    try: js = json.loads(str(data))
    except: js=None
    if 'status' not in js or js['status'] != 'OK':
        print '====Failure to Retrieve===='
        print data
        continue
    lat = js["results"][0]["geometry"]["location"]["lat"]
    lng = js["results"][0]["geometry"]["location"]["lng"]
    print 'lat', lat, 'lng', lng
    location = js["results"][0]["formatted_address"]
    print location


dgelessus

Is this exactly what your code looks like? All the indentation appears to be misisng, and in Python the indentation is actually required, not just good style.

If possible, can you post the code in a code block? To do that, hit enter twice, type "```python" (without the double quotes, but with the backticks), hit enter and paste your code, then hit enter again and type "```" (again, without the double quotes).

brumm

No API key is necessary.

Enter location : france
Retrieving http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=france
Retrieved 1181 characters
lat 46.227638 lng 2.213749
France
Enter location : paris
Retrieving http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=paris
Retrieved 9099 characters
lat 48.856614 lng 2.3522219
Paris, France
Enter location :

ywangd

The code works for me. No error at all.

JonB

If you click on the Syntax Error popup, it should tell you a line number. Then, start checking the lines behind. Usually this means you are missing a parenthesis or close quote on a previous line. You might try copying the code from above back into pythonista... non ascii error implies there is a hidden non ascii character, which maybe was stripped by
the forums, you might also try including the
# coding: utf-8
comment at the top.

WayneJang

I followed the above suggestions - and I deleted all 'white spaces' below my code and it worked fine. Maybe, there were some hidden ASCII codes. Thanks all for great advices.