translate() 在 python 错误中只需要一个参数(给出 2 个)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39375712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
translate() takes exactly one argument (2 given) in python error
提问by vikash
import os
import re
def rename_files():
# get the files from dir
file_list=os.listdir(r"C:\OOP\prank")
print(file_list)
saved_path=os.getcwd()
print("current working directory"+saved_path)
os.chdir(r"C:\OOP\prank")
#rename the files
for file_name in file_list:
print("old name-"+file_name)
#print("new name-"+file_name.strip("0123456789"))
os.rename(file_name,file_name.translate(None,"0123456789"))
os.chdir(saved_path)
rename_files()
Here error is showing due to translate line ...help me what to do next ..I am using translate to remove the digit from filename.
由于翻译行,这里显示错误......帮助我下一步做什么......我正在使用翻译从文件名中删除数字。
Traceback (most recent call last):
File "C:\Users\vikash\AppData\Local\Programs\Python\Python35- 32\pythonprogram\secretName.py", line 17, in <module>
rename_files()
File "C:\Users\vikash\AppData\Local\Programs\Python\Python35- 32\pythonprogram\secretName.py", line 15, in rename_files
os.rename(file_name,file_name.translate(None,"0123456789"))
TypeError: translate() takes exactly one argument (2 given)
回答by Dunes
str.translate
requires a dict
that maps unicode ordinals to other unicode oridinals (or None
if you want to remove the character). You can create it like so:
str.translate
需要dict
将 unicode ordinals 映射到其他 unicode oridinals(或者None
如果您想删除字符)。您可以像这样创建它:
old_string = "file52.txt"
to_remove = "0123456789"
table = {ord(char): None for char in to_remove}
new_string = old_string.translate(table)
assert new_string == "file.txt"
However, there is simpler way of making a table though, by using the str.maketrans
function. It can take a variety of arguments, but you want the three arg form. We ignore the first two args as they are for mapping characters to other characters. The third arg is characters you wish to remove.
但是,通过使用该str.maketrans
函数,还有一种更简单的制作表格的方法。它可以采用多种参数,但您需要三个 arg 形式。我们忽略前两个参数,因为它们用于将字符映射到其他字符。第三个参数是您要删除的字符。
old_string = "file52.txt"
to_remove = "0123456789"
table = str.maketrans("", "", to_remove)
new_string = old_string.translate(table)
assert new_string == "file.txt"
回答by Venkat
Higher versions in Python use this :
Python 中的更高版本使用这个:
eg: oldname= "delhi123"
remove="1234567890"
table=str.maketrans("","",remove)
oldname.translate(table)
Overall solution for your query:
您的查询的整体解决方案:
import os
def rename_file_names():
file_list=os.listdir(r"C:\Users\welcome\Downloads\Compressed\prank")
print (file_list)
saved_path=os.getcwd()
print("current working direcorty is"+saved_path)
os.chdir(r"C:\Users\welcome\Downloads\Compressed\prank")
remove="123456789"
table=str.maketrans("","",remove)
for file_name in file_list:
os.rename(file_name,file_name.translate(table))
rename_file_names()
回答by SUMIT RAJ
Change os.rename(file_name,file_name.translate(None,"0123456789"))
to os.rename(file_name,file_name.translate(str.maketrans('','',"0123456789")))
and it will work.
更改os.rename(file_name,file_name.translate(None,"0123456789"))
为 os.rename(file_name,file_name.translate(str.maketrans('','',"0123456789")))
,它将起作用。
回答by nancy agarwal
This is how translate works:
这就是翻译的工作方式:
yourstring.translate(str.maketrans(fromstr, tostr, deletestr));
Replace the characters in fromstr with the character in the same position in tostr and delete all characters that are in deletestr. The fromstr and tostr can be empty strings and the deletestr parameter can be omitted.
用tostr中相同位置的字符替换fromstr中的字符,并删除deletestr中的所有字符。fromstr 和 tostr 可以是空字符串,并且可以省略 deletestr 参数。
回答by Kinchit
import os
def rename_files():
#1 get file names from folder
list_files = os.listdir(r"C:\Personal\Python\prank")
print(list_files)
saved_path = os.getcwd()
print(os.getcwd())
os.chdir(r"C:\Personal\Python\prank\")
#2 for each file, rename filename
remove = "0123456789"
table = str.maketrans("","",remove)
for file_name in list_files:
os.rename(file_name, file_name.translate(table))
os.chdir(saved_path)
rename_files()
回答by Almadani
instead of using function getcwd() it just works
而不是使用函数 getcwd() 它只是工作
import os
def rename_files():
#(1) get file names from a folder
file_list = os.listdir(r"D:\prank\prank")
os.chdir(r"D:\prank\prank")
#remove any numbers
remove = "0123456789"
chart = str.maketrans("","", remove)
# files name renamed
for file_name in file_list:
os.rename(file_name, file_name.translate(chart))
print(file_name)
回答by Yashi Aggarwal
If all you are looking to accomplish is to do the same thing you were doing in Python 2 in Python 3, here is what I was doing in Python 2.0 to throw away punctuation and numbers:
如果您想要完成的只是在 Python 3 中做与在 Python 2 中所做的相同的事情,那么我在 Python 2.0 中所做的就是丢弃标点符号和数字:
text = text.translate(None, string.punctuation)
text = text.translate(None, '1234567890')
Here is my Python 3.0 equivalent:
这是我的 Python 3.0 等效项:
text = text.translate(str.maketrans('','',string.punctuation))
text = text.translate(str.maketrans('','','1234567890'))
Basically it says 'translate nothing to nothing' (first two parameters) and translate any punctuation or numbers to None (i.e. remove them).
基本上它说“什么都不翻译”(前两个参数)并将任何标点符号或数字翻译为“无”(即删除它们)。
回答by Archana
Run the file with the command python secret_message.py . It works.
使用命令 python secret_message.py 运行该文件。有用。
here is the entire code:
这是整个代码:
import os
def rename_files(): #(1)get file names from a folder file_list = os.listdir(r"/Users/archananagaraja/Desktop/AN_Personal/Udacity_Python/prank") print(file_list) current_dir = os.getcwd() print("My current directory is: "+current_dir) os.chdir(r"/Users/archananagaraja/Desktop/AN_Personal/Udacity_Python/prank") #(2) for each file rename filename for file_name in file_list: os.rename(file_name, file_name.translate(None, "0123456789")) rename_files()
def rename_files(): #(1) 从文件夹中获取文件名 file_list = os.listdir(r"/Users/archananagaraja/Desktop/AN_Personal/Udacity_Python/prank") print(file_list) current_dir = os.getcwd() print( "我当前的目录是:"+current_dir) os.chdir(r"/Users/archananagaraja/Desktop/AN_Personal/Udacity_Python/prank") #(2) 为每个文件重命名 file_list 中的 file_name 文件名: os.rename(file_name, file_name.translate(None, "0123456789")) rename_files()
回答by MooingRawr
Instead of translate why not just do this:
而不是翻译为什么不这样做:
os.rename(file_name,''.join([i for i in file_name if not i.isdigit()]))
回答by FiFi.X
if you use the python 3.X,try this:file_name.lstrip()
如果你使用 python 3.X,试试这个:file_name.lstrip()
os.rename(file_name,file_name.lstrip(None,"0123456789"))