From fc8c521daade82d3e7359a9255753c6f4513d0b4 Mon Sep 17 00:00:00 2001 From: Frederik Elwert Date: Sun, 29 Sep 2019 14:05:04 +0200 Subject: [PATCH 1/4] Convert Python code to Py3. --- en/lessons/transliterating.md | 90 +++++++++++++++++------------------ 1 file changed, 45 insertions(+), 45 deletions(-) mode change 100755 => 100644 en/lessons/transliterating.md diff --git a/en/lessons/transliterating.md b/en/lessons/transliterating.md old mode 100755 new mode 100644 index cf09e0ecd..410bf7c7e --- a/en/lessons/transliterating.md +++ b/en/lessons/transliterating.md @@ -125,17 +125,17 @@ our purposes, what is important is that the encoding is stored under the ``` python #transliterator.py -import urllib2 +from urllib.request import urlopen -page = urllib2.urlopen('http://lists.memo.ru/d1/f1.htm') +page = urlopen('http://lists.memo.ru/d1/f1.htm') #what is the encoding? -print page.headers['content-type'] +print(page.headers['content-type']) ``` Under the ‘content-type’ key we find this information: -``` python +``` text/html; charset=windows-1251 ``` @@ -179,7 +179,7 @@ encoding = page.headers['content-type'].split('charset=')[1] The encoding is assigned to the variable called ‘*encoding*’. You can check to see if this worked by printing the ‘*encoding*’ variable. Now we can tell Python how to read the page as Unicode. Using the -`unicode(object [, encoding])` method turns a string of characters into a +`str(object [, encoding])` method turns a string of characters into a Unicode object. A Unicode object is similar to a string but it can contain special characters. If they are in a non-ASCII character set, like here with ‘windows-1251’, we have to use the optional encoding @@ -190,7 +190,7 @@ parameter. content = page.read() # the unicode method tries to use ASCII so we need to tell it the encoding -content = unicode(content, encoding) +content = str(content, encoding) content[200:300] ``` @@ -212,12 +212,12 @@ than Unicode: ``` python # see what happens when Python prints Unicode -print content[200:300] +print(content[200:300]) ```   -``` python +``` "list-right">