python - Python 3 中的翻译函数

标签 python function translate

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

这是不起作用的代码块:

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

谢谢

最佳答案

str.translate 还在,只是界面变了一点:

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

关于python - Python 3 中的翻译函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41708770/

相关文章:

Java Graphics2D 平移和缩放/平移和缩放

javascript - 如何将此加密函数从 PHP 转换为 Javascript

python - 运行时错误 : The following handlers are available to decode the pixel data however they are missing required dependencies: GDCM (req. GDCM)

c# - 用于解析 C/C++ 函数声明的正则表达式

lisp - 在 clojure 中让 vs def

PHP Delete 返回错误

Javascript - 从当前位置移动框(推荐 slider )

python - 如何在Python中迭代一个大文本文件(906.2MB)?

python - 错误 : Failed to load the native TensorFlow runtime in Python 2. 7

python - 无法解析来自 `th` 标记的数据以及来自不同表的 `td` 标记