1

I'm a python beginner (working only with python3 so far) and I'm trying to present some code working the curses library to my classmates.

I got the code from a python/curses tutorial and it runs without problems in python2. In python3 it doesn't and I get the error in title. Searching through the already asked questions, I found several solutions to this, but since being a absolute beginner with coding, I have no idea how to execute those in my specific code.

This is the code working in python2 :

import curses
from urllib2 import urlopen
from HTMLParser import HTMLParser
from simplejson import loads


def get_new_joke():

joke_json = loads(urlopen('http://api.icndb.com/jokes/random').read())
return HTMLParser().unescape(joke_json['value']['joke']).encode('utf-8')

Using the new modules in python3:

import curses
import json
import urllib
from html.parser import HTMLParser



def get_new_joke():

    joke_json = loads(urlopen('http://api.icndb.com/jokes/random').read())
    return HTMLParser().unescape(joke_json['value']['joke']).encode('utf-8')

Furthermore I tried to include this solution into my code: Python 3, let json object accept bytes or let urlopen output strings

response = urllib.request.urlopen('http://api.icndb.com/jokes/random')
str_response = joke_json.readall().decode('utf-8')
obj = json.loads(str_response)

Tried around for hours now, but it tells me "json" ist not defined.

Community
  • 1
  • 1
fayme11
  • 11
  • 1
  • 2

0 Answers0