Skip to content

Commit 9c0de6e

Browse files
committed
Python3 compatibility in __main__
I tend to use this and got frustrated when it stopped working in python3. Fixed urllib and print issues to be 2to3 compliant.
1 parent 3004eb8 commit 9c0de6e

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

microdata.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,13 @@ def _make_item(e):
226226

227227

228228
if __name__ == "__main__":
229-
import urllib
229+
try:
230+
from urllib.request import urlopen
231+
except ImportError:
232+
from urllib import urlopen
233+
230234
if len(sys.argv) < 2:
231-
print "Usage: %s URL [...]" % sys.argv[0]
235+
print("Usage: %s URL [...]" % sys.argv[0])
232236
sys.exit(1)
233237

234238
for url in sys.argv[1:]:
@@ -237,7 +241,7 @@ def _make_item(e):
237241
microdata = {}
238242
microdata['items'] = items = []
239243

240-
for item in get_items(urllib.urlopen(url)):
244+
for item in get_items(urlopen(url)):
241245
items.append(item.json_dict())
242246

243-
print json.dumps(microdata, indent=2)
247+
print(json.dumps(microdata, indent=2))

0 commit comments

Comments
 (0)