1

i am trying to create and display arrays from a string value. First i try to create a JSON value from the string, then i try to display key/values from it.

I get the error: Invalid syntax on last rule... busy whole evening, but can't find dit out:(

Python3 code:

import urllib.request
import urllib.parse
import json

user = 'user'
pw = 'password'

login_url = 'http://www.xxxxxxx.nl/test/index.php'

data = urllib.parse.urlencode({'user': user, 'pw': pw})
data = data.encode('utf-8')
# adding charset parameter to the Content-Type header.
request = urllib.request.Request(login_url)
request.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8")

f = urllib.request.urlopen(request, data)

## returning content from webpage: {"A":"aap","B":"noot","C":"mies"}

#json.JSONDecoder().decode(f)
#json.JSONDecoder.raw_decode(f)
#test = print(f.read().decode('utf-8'))
test = f.read().decode('utf-8')
#print (test)
#test2 = json.JSONDecoder().decode(test)

test = '{"A":"aap","B":"noot","C":"mies"}'
dest = json.loads(test)
print dest['A']  ## <<< invalid syntax ??

#json.loads(test2)
1
  • 1
    Side note: You're not creating a "JSON object" from a string. There's no such thing as a JSON object. It's the string that's JSON. You're creating a dict (not even some special kind of dict) from the JSON string representation of a dictionary/object. Commented Apr 22, 2013 at 22:11

1 Answer 1

3

In Python 3.x - print is a function (ie: no longer a statement as in Python 2.x) - you need to use:

print(dest['A'])

instead of:

print dest['A'] 
Sign up to request clarification or add additional context in comments.

1 Comment

aaaaaaaaaaaarrrrrrrrrrgggg...spent hours on it!!! Life could be soooo easy:) Thank you!!!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.