本文介绍了在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中翻译函数
发布评论