首页 > 编程问答 >为什么 string.maketrans 在 Python 3.1 中不起作用?

为什么 string.maketrans 在 Python 3.1 中不起作用?

时间:2024-07-30 17:12:11浏览次数:6  
标签:python python-3.x

我是 Python 新手。

怎么了 这个 在 Python 3.1 中不起作用?

from string import maketrans   # Required to call maketrans function.

intab = "aeiou"
outtab = "12345"
trantab = maketrans(intab, outtab)

str = "this is string example....wow!!!";
print str.translate(trantab);

当我执行上面的代码时,我得到以下信息:

Traceback (most recent call last):
  File "<pyshell#119>", line 1, in <module>
    transtab = maketrans(intab, outtab)
  File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/string.py", line 60, in maketrans
    raise TypeError("maketrans arguments must be bytes objects")
TypeError: maketrans arguments must be bytes objects

“必须”是什么意思是字节对象”是什么意思?如果可能的话,有人可以帮助发布 Python 3.1 的工作代码吗?


在 Python 3 中, str.maketrans 已被移除。在 Python 3 中, bytes.maketrans str.maketrans 方法的等效方法是作为静态方法内置于 str 类型中:

str.maketrans(x[, y[, z]])

如果传入一个参数,则它必须是具有 1 对 1 映射的 Unicode 序数(整数)或字符的字典映射。如果传入两个参数,则它们都必须是长度相等的字符串,并且生成的翻译表将把来自第一个字符串的字符映射到来自第二个字符串的字符。如果传入第三个参数,则它必须是一个字符串(表示要从翻译中删除的字符),并且生成的翻译表将所有出现的字符映射到 None

以下是工作示例:

# -*- coding: utf-8 -*-

intab = "aeiou"
outtab = "12345"
trantab = str.maketrans(intab, outtab)   # 这一步创建了转换表

str = "this is string example....wow!!!"
print(str.translate(trantab));

这将打印:

th3s 3s str3ng 2x1mpl2....w4w!!!

有关详细信息,请参阅 Python 3.1.1 文档

标签:python,python-3.x
From: 3031045

相关文章

  • Python - Creating alternative initializers using class Methods
    Classmethodsallowustodefinealternativeinitializers(alsoknownasfactorymethods)inaclass.Thesemethodshelpuscreateinstanceobjectsfromdifferenttypesofinputdata.Letusunderstandthiswiththehelpofanexample.Again,wetakethe......
  • 如何让 Python 请求信任自签名 SSL 证书?
    importrequestsdata={'foo':'bar'}url='https://foo.com/bar'r=requests.post(url,data=data)如果URL使用自签名证书,则会失败requests.exceptions.SSLError:[Errno1]_ssl.c:507:error:14090086:SSLroutines:SSL3_GET_SERVER_CERTIF......
  • python 偏函数
    如下代码loop=tornado.ioloop.IOLoop.current()ctx=contextvars.copy_context()func_call=functools.partial(ctx.run,func,*args,**kwargs)returnawaitloop.run_in_executor(executor,func_call)偏函数一个函数作为模板,通过提供部分参数来产生一个新的函数。......
  • Chapter 18 Python异常
    欢迎大家订阅【Python从入门到精通】专栏,一起探索Python的无限可能!文章目录前言一、什么是异常二、捕获异常三、异常的传递前言在Python中,异常是一种特定的对象,能够在程序运行过程中被抛出和处理。有效地管理异常不仅可以增强程序的稳定性,还可以提高用户体验,使程......
  • Python正则表达式匹配数字的第一次重复
    示例:For0123123123,1应匹配,因为第二个1出现在任何其他数字重复之前。For01234554321,5应该匹配,因为第二个5出现在任何其他数字的重复之前。我尝试过的一些正则表达式:......
  • 当 python 极坐标中某些列条目为空时,如何分解 List[_] 列?
    给定如下所示的Polarsdf,如何在两列上调用explode(),同时将空条目扩展到正确的长度以与其行匹配?shape:(3,2)┌───────────┬─────────────────────┐│x┆y││---┆---......
  • 使用python从网站自动下载pdf时出错
    我想从一个名为epadossier.nl的网站自动批量下载pdf我用谷歌搜索了这个并找到了一段代码并修复了一个小错误。所以现在我得到了这个`importrequestsurl="https://www.epadossier.nl/adres/plaats/straat/num"response=requests.get(url)ifresponse.status_cod......
  • 避免字符串连接的嵌套循环的 Pythonic 方法
    我想找到所有5位数字的字符串,其中前三位数字在我的第一个列表中,第二个槽第四个数字在我的第二个列表中,第三到第五个数字在我的最后一个列表中:l0=["123","567","451"]l1=["234","239","881"]l2=["348","551","399"......
  • Python 环境配置(二)安装jupyter、matplotlib、numpy库
    Python环境配置(二)安装jupyter、matplotlib、numpy库一、numpypipinstallnumpy二、matplotlibpipinstallmatplotlib三、jupyter1、anaconda自带Jupyter2、pycharm插件只有Pycharm的Professional版才支持JupyterNotebook,请注意版本3、新建文件#%......
  • 如何使用 PIPE 并行运行 python 子进程?
    我正在使用inkscape将一堆SVG图像转换为PNG。单线程:importsubprocessimporttimeimportosinkscape_path=r'C:\ProgramFiles\Inkscape\bin\inkscape.com'steps=30filenames=[]processes=[]#t_start=time.process_time()t_start=time.time()f......