Python string.replace() 不替换字符

发布于 2024-09-15 19:34:40 字数 787 浏览 5 评论 0原文

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

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

bad_characters = ["/", "\\", ":", "(", ")", "<", ">", "|", "?", "*"]
for letter in bad_characters:
    filename = line[2].replace(letter, "_")
    foldername = line[5].replace(letter, "_")
  • line[2] 示例:“Blah blah Boring - Meeting 2/19/2008.doc"
  • line[5] 示例:“商务会议 2/2008”

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

我在这里做错了什么吗?

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.

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"

When I add print letter inside of the for loop, 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.

Is there anything I'm doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烟若柳尘 2024-09-22 19:34:40

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

你应该使用:

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

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

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

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

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

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])
心安伴我暖 2024-09-22 19:34:40

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

You are reassigning to the filename and foldername variables at every iteration of the loop. In effect, only * is being replaced.

时间你老了 2024-09-22 19:34:40

你应该看看 python 字符串方法 translate()
http://docs.python.org/library/string.html#string。翻译

http://docs.python.org/library/string.html#string。 maketrans

Editing this to add an example as per comment suggestion below:

import string
toreplace=''.join(["/", "\\", ":", "(", ")", "<", ">", "|", "?", "*"]) 
underscore=''.join( ['_'] * len(toreplace))
transtable = string.maketrans(toreplace,underscore)
filename = filename.translate(transtable)
foldername = foldername.translate(transtable)

可以通过替换诸如“/\:”之类的内容来简化,我只是使用了上面给出的内容

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

Editing this to add an example as per comment suggestion below:

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

清醇 2024-09-22 19:34:40

您将从基线开始,而不是保存替换的结果,因此您将得到相当于

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

尝试以下内容的结果

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

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, "_")
雅心素梦 2024-09-22 19:34:40

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

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

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

bad_characters = ["/", "\\", ":", "(", ")", "<", ">", "|", "?", "*"]
for letter in bad_characters:
    filename = string.replace(line[2], letter, "_")
    foldername = string.replace(line[5], letter, "_")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文