在Python 3中翻译函数

编程入门 行业动态 更新时间:2024-10-21 06:43:33
本文介绍了在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中翻译函数

本文发布于:2023-10-27 11:16:40,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!