您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何解决'str'在Python中没有属性'maketrans'错误?

如何解决'str'在Python中没有属性'maketrans'错误?

您正在使用Python 2运行为Python 3编写的代码。这将无法工作。

maketransbytes内置类型的类方法,但 仅在Python 3中

# Python 3
>>> bytes
<class 'bytes'>
>>> bytes.maketrans
<built-in method maketrans of type object at 0x10aa6fe70>

在Python 2,bytes一个别名str,但该类型并 有方法

# Python 2.7
>>> bytes
<type 'str'>
>>> bytes.maketrans
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'str' has no attribute 'maketrans'

而是使用Python 3运行您的代码,或将该项目中的所有代码转换为Python 2;后者需要深入了解Python 2和3的不同之处,这可能是一项重要的工作。

只是 翻译成Python 2的所示函数将是:

import string
import urllib2
import base64
import random

def get_appids():
    fly = string.maketrans(
        "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
        "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"
    )
    f = urllib2.urlopen("http://lovejiani.com/v").read().translate(fly)
    d = base64.b64decode(f)
    e = unicode(d, encoding='ascii').split(u'\r\n')
    random.shuffle(e)
    return e
python 2022/1/1 18:31:16 有191人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶