Python3 错误:TypeError:无法将“字节”对象隐式转换为 str

Posted

技术标签:

【中文标题】Python3 错误:TypeError:无法将“字节”对象隐式转换为 str【英文标题】:Python3 Error: TypeError: Can't convert 'bytes' object to str implicitly 【发布时间】:2013-05-17 22:52:34 【问题描述】:

我正在 learnpythonthehardway 中的练习 41 并不断收到错误:

  Traceback (most recent call last):
  File ".\url.py", line 72, in <module>
    question, answer = convert(snippet, phrase)
  File ".\url.py", line 50, in convert
    result = result.replace("###", word, 1)
TypeError: Can't convert 'bytes' object to str implicitly

我使用的是python3,而书籍使用的是python2,所以我做了一些更改。这是脚本:

#!/usr/bin/python
# Filename: urllib.py

import random
from random import shuffle
from urllib.request import urlopen
import sys

WORD_URL = "http://learncodethehardway.org/words.txt"
WORDS = []

PHRASES = 
            "class ###(###):":
                "Make a class named ### that is-a ###.",
            "class ###(object):\n\tdef __init__(self, ***)" :
                "class ### has-a __init__ that takes self and *** parameters.",
            "class ###(object):\n\tdef ***(self, @@@)":
                "class ### has-a funciton named *** that takes self and @@@ parameters.",
            "*** = ###()":
                "Set *** to an instance of class ###.",
            "***.*** = '***'":
                "From *** get the *** attribute and set it to '***'."


# do they want to drill phrases first
PHRASE_FIRST = False
if len(sys.argv) == 2 and sys.argv[1] == "english":
    PHRASE_FIRST = True

# load up the words from the website
for word in urlopen(WORD_URL).readlines():
    WORDS.append(word.strip())

def convert(snippet, phrase):
    class_names = [w.capitalize() for w in
                    random.sample(WORDS, snippet.count("###"))]
    other_names = random.sample(WORDS, snippet.count("***"))
    results = []
    param_names = []

    for i in range(0, snippet.count("@@@")):
        param_count = random.randint(1,3)
        param_names.append(', '.join(random.sample(WORDS, param_count)))

    for sentence in snippet, phrase:
        result = sentence[:]

        # fake class names
        for word in class_names:
            result = result.replace("###", word, 1)

        # fake other names
        for word in other_names:
            result = result.replace("***", word, 1)

        # fake parameter lists
        for word in param_names:
            result = result.replace("@@@", word, 1)

        results.append(result)

    return results

# keep going until they hit CTRL-D
try:
    while True:
        snippets = list(PHRASES.keys())
        random.shuffle(snippets)

        for snippet in snippets:
            phrase = PHRASES[snippet]
            question, answer = convert(snippet, phrase)
            if PHRASE_FIRST:
                question, answer = answer, question

            print(question)

            input("> ")
            print("ANSWER: \n\n".format(answer))
except EOFError:
    print("\nBye")

我到底做错了什么?谢谢!

【问题讨论】:

附带说明,当您从 urllib 包导入文件时,将文件命名为 urllib.py 是一个非常落后的想法。但这不是你的问题。 另外,for word in urlopen(WORD_URL).readlines(): 很傻;就做for word in urlopen(WORD_URL):。我假设您是从您正在关注的教程中获得的,这意味着该教程不仅是为 python2 编写的,而且是为非常古老的 python2 编写的(或者至少是由习惯于非常古老的 python2 的人编写的),所以......如果你想学习如何编写现代的、惯用的 Python,你可能想找到一个更新的教程。 我在这个链接中找到了解释(和提供的解决方案):mkyong.com/python/… 非常有帮助。 【参考方案1】:

urlopen() 返回一个字节对象,要对其执行字符串操作,您应该先将其转换为str

for word in urlopen(WORD_URL).readlines():
    WORDS.append(word.strip().decode('utf-8')) # utf-8 works in your case

要获得正确的字符集:How to download any(!) webpage with correct charset in python?

【讨论】:

除非数据实际上不是 UTF-8。你在这里很幸运,因为它恰好是 ASCII,它是 UTF-8 的严格子集,但假设你在任何地方都会如此幸运是不好的。【参考方案2】:

在 Python 3 中,urlopen function 返回一个 HTTPResponse 对象,其作用类似于二进制文件。所以,当你这样做时:

for word in urlopen(WORD_URL).readlines():
    WORDS.append(word.strip())

...你最终会得到一堆 bytes 对象而不是 str 对象。所以当你这样做时:

result = result.replace("###", word, 1)

...您最终尝试将字符串result 中的字符串"###" 替换为bytes 对象,而不是str。因此错误:

TypeError: Can't convert 'bytes' object to str implicitly

答案是在获得单词后立即对其进行显式解码。为此,您必须从 HTTP 标头中找出正确的编码。你是怎么做到的?

在这种情况下,我阅读了标题,我可以看出它是 ASCII,而且它显然是一个静态页面,所以:

for word in urlopen(WORD_URL).readlines():
    WORDS.append(word.strip().decode('ascii'))

但在现实生活中,您通常需要编写代码来读取标题并动态计算出来。或者,更好的是,安装一个更高级别的库,如 requests,does that for you automatically。

【讨论】:

【参考方案3】:

将字节类型'word'显式转换为字符串

result = result.replace("###", sre(word), 1)

它应该可以工作

【讨论】:

以上是关于Python3 错误:TypeError:无法将“字节”对象隐式转换为 str的主要内容,如果未能解决你的问题,请参考以下文章

python3错误之TypeError: 'dict_items' object is not callable

“ TypeError: 'int' object is not callable” python3中的独特问题

多处理,Python3,Windows:TypeError:无法腌制 _thread.lock 对象

收到错误“创建 HelloWorld 错误:TypeError:无法将未定义或 null 转换为对象”

TypeError:“map”类型的对象没有 len() Python3

TypeError:无法将“4”解释为数据类型