在Python 3中翻译函数 [英] Translate function in Python 3

查看:241
本文介绍了在Python 3中翻译函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Python 3,我想将我的文件名翻译为没有数字。翻译功能在Python 3中似乎不起作用。如何将文件名翻译为没有数字?

I am using Python 3 and I want to translate my file names to have no numbers. The translate function doesn't seem to work in Python 3. How can I translate the file names to have no numbers?

这是不起作用的代码块:

This is the block of code that doesn't work:

file_name = 123hello.jpg
file_name.translate(None, "0123456789")

谢谢

推荐答案

str.translate 仍然存在,界面刚刚改变了一点:

str.translate is still there, the interface has just changed a little:

>>> table = str.maketrans(dict.fromkeys('0123456789'))
>>> '123hello.jpg'.translate(table)
'hello.jpg'

这篇关于在Python 3中翻译函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆