Python string.replace() 不替换字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3523054/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:33:18  来源:igfitidea点击:

Python string.replace() not replacing characters

pythonstringstr-replace

提问by ZeroUptime

Some background information: We have an ancient web-based document database system where I work, almost entirely consisting of MS Office documents with the "normal" extensions (.doc, .xls, .ppt). They are all named based on some sort of arbitrary ID number (i.e. 1245.doc). We're switching to SharePoint and I need to rename all of these files and sort them into folders. I have a CSV file with all sorts of information (like which ID number corresponds to which document's title), so I'm using it to rename these files. I've written a short Python script that renames the ID number title.

一些背景信息:我工作的地方有一个古老的基于 Web 的文档数据库系统,几乎完全由带有“普通”扩展名(.doc、.xls、.ppt)的 MS Office 文档组成。它们都是根据某种任意 ID 号(即 1245.doc)命名的。我们正在切换到 SharePoint,我需要重命名所有这些文件并将它们分类到文件夹中。我有一个包含各种信息的 CSV 文件(比如哪个 ID 号对应哪个文档的标题),所以我用它来重命名这些文件。我编写了一个简短的 Python 脚本来重命名 ID 号标题。

However, some of the titles of the documents have slashes and other possibly bad characters to have in a title of a file, so I want to replace them with underscores:

但是,文档的某些标题在文件标题中包含斜杠和其他可能的坏字符,因此我想用下划线替换它们:

bad_characters = ["/", "\", ":", "(", ")", "<", ">", "|", "?", "*"]
for letter in bad_characters:
    filename = line[2].replace(letter, "_")
    foldername = line[5].replace(letter, "_")
  • Example of line[2]: "Blah blah boring - meeting 2/19/2008.doc"
  • Example of line[5]: "Business meetings 2/2008"
  • 示例line[2]:“废话无聊 - 会议 2/19/2008.doc”
  • 示例line[5]:“商务会议 2/2008”

When I add print letterinside of the forloop, it will print out the letter it's supposed to be replacing, but won't actually replace that character with an underscore like I want it to.

当我print letterfor循环内部添加时,它会打印出它应该替换的字母,但实际上不会像我想要的那样用下划线替换该字符。

Is there anything I'm doing wrong here?

我在这里做错了什么吗?

采纳答案by NullUserException

That's because filenameand foldernameget thrown away with each iteration of the loop. The .replace()method returns a string, but you're not saving the result anywhere.

那是因为filenameandfoldername会随着循环的每次迭代而被丢弃。该.replace()方法返回一个字符串,但您没有将结果保存在任何地方。

You should use:

你应该使用:

filename = line[2]
foldername = line[5]

for letter in bad_characters:
    filename = filename.replace(letter, "_")
    foldername = foldername.replace(letter, "_")

But I would do it using regex. It's cleaner and (likely) faster:

但我会使用正则表达式来做到这一点。它更干净并且(可能)更快:

p = re.compile('[/:()<>|?*]|(\\)')
filename = p.sub('_', line[2])
folder = p.sub('_', line[5])

回答by Vebjorn Ljosa

You are reassigning to the filenameand foldernamevariables at every iteration of the loop. In effect, only *is being replaced.

您将在循环的每次迭代中重新分配给filenamefoldername变量。实际上,只是*被替换了。

回答by Amala

You should look at the python string method translate()http://docs.python.org/library/string.html#string.translatewith http://docs.python.org/library/string.html#string.maketrans

你应该看看蟒蛇字符串的方法translate()http://docs.python.org/library/string.html#string.translatehttp://docs.python.org/library/string.html#string.maketrans

编辑此内容以根据以下评论建议添加示例:
import string
toreplace=''.join(["/", "\", ":", "(", ")", "<", ">", "|", "?", "*"]) 
underscore=''.join( ['_'] * len(toreplace))
transtable = string.maketrans(toreplace,underscore)
filename = filename.translate(transtable)
foldername = foldername.translate(transtable)

Can simplify by making the toreplace something like '/\:,' etc, i just used what was given above

可以通过使 toreplace 类似 '/\:,' 等来简化,我只是使用了上面给出的内容

回答by Kathy Van Stone

You are starting over with the base line instead of saving the replaced result, thus you are getting the equivalent to

您从基线重新开始而不是保存替换的结果,因此您将获得等效于

filename = line[2].replace('*', '_')
foldername = line[5].replace('*', '_')

Try the following

尝试以下

bad_characters = ["/", "\", ":", "(", ")", "<", ">", "|", "?", "*"]
filename = line[2]
foldername = line[5]
for letter in bad_characters:
    filename = filename.replace(letter, "_")
    foldername = foldername.replace(letter, "_")

回答by wu liang

Should use string.replace(str, fromStr, toStr)

应该使用 string.replace(str, fromStr, toStr)

bad_characters = ["/", "\", ":", "(", ")", "<", ">", "|", "?", "*"]
for letter in bad_characters:
    filename = string.replace(line[2], letter, "_")
    foldername = string.replace(line[5], letter, "_")