Python3 urlopen() TypeError: can’t convert ‘bytes’ object to str im…

recently wrote the program using urllib. Request urlopen to read pages of text files, the results appeared a TypeError, problems and solving methods will now be recorded as follows, hope to help those encountered similar problems:

uses the Python version 3.5.2

 1 # -*- coding: UTF-8 -*-
 2 #!/usr/bin/python
 3 # Filename: urllib.py
 4 import random
 5 from urllib.request import urlopen
 6 import sys
 7 
 8 WORD_URL = "http://learncodethehardway.org/words.txt"
 9 WORDS = []
10 
11 PHRASES = {
12         " class %%%(%%%):":
13             "Make a class named %%% that is-a %%%.",
14         "class %%%(object):\n\tdef__init__(self,***)":
15             "class %%% has-a __init__ that takes self and *** parameters.",
16         "*** = %%%()":
17             "Set *** to an instance of class %%%.",
18         "***.***(@@@)":
19             "From *** get the *** function, and call it with parameters self,@@@.",
20         "***.*** = '***'":
21             "From *** get the *** attribute and set it to '***'."
22             }
23             
24 PHRASES_FIRST = False
25 if len(sys.argv) == 2 and sys.argv[1] == "english":
26     PHRASES_FIRST = True
27                 
28 for word in urlopen(WORD_URL).readlines():
29     WORDS.append(word.strip())
30                 
31 def convert(snippet, phrase):
32     class_names = [w.capitalize() for w in
33                 random.sample(WORDS, snippet.count("%%%"))]
34     other_names = random.sample(WORDS, snippet.count("***"))
35     results = []
36     param_names = []
37                 
38     for i in range(0, snippet.count("@@@")):
39         param_count = random.randint(1,3)
40         param_names.append(', '.join(random.sample(WORDS, param_count)))
41         #param_names.append(', '.join('%s' %id for id in random.sample(WORDS, param_count)))
42                     
43     for sentence in snippet, phrase:
44         result = sentence[:]
45         print(result)
46                     
47         for word in class_names:
48             result = result.replace("%%%", word, 1)
49 
50         for word in other_names:
51             result = result.replace("***",word, 1)
52                     
53         for word in param_names:
54             result = result.replace("@@@", word, 1)
55                         
56         results.append(result)
57                 
58     return results
59         
60 try:
61     while True:
62         snippets = list(PHRASES.keys())
63         random.shuffle(snippets)
64         
65         for snippet in snippets:
66             phrase = PHRASES[snippet]
67             question, answer  = convert(snippet, phrase)
68             if PHRASES_FIRST:
69                 question, answer = answer,question
70                 
71             print (question)
72             
73             input ("> ")
74             print ("ANSWER: %s\n\n" % answer)
75 except EOFError:
76     print ("\nBye!")

= TypeError: Can’t convert ‘bytes’ object to STR IMPLICITLY

after the access to information found that urlopen () returns a bytes object, if you need to string operations, he need to explicitly convert it into a string, can appear otherwise the above problems.

we when reading web content directly convert them to encoding to utf-8, can continue to use. Change line 29 of the above code to

WORDS.append(word.strip().decode('utf-8'))

problem solved.

Similar Posts: