Python string.replace() 不替换字符
一些背景信息: 我工作的地方有一个古老的基于网络的文档数据库系统,几乎完全由具有“正常”扩展名(.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是因为
filename
和foldername
在循环的每次迭代中都会被丢弃。.replace()
方法返回一个字符串,但您没有将结果保存在任何地方。你应该使用:
但我会使用正则表达式来做到这一点。它更干净并且(可能)更快:
That's because
filename
andfoldername
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:
But I would do it using regex. It's cleaner and (likely) faster:
您将在循环的每次迭代中重新分配给
filename
和foldername
变量。实际上,只有*
被替换。You are reassigning to the
filename
andfoldername
variables at every iteration of the loop. In effect, only*
is being replaced.你应该看看 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:
可以通过替换诸如“/\:”之类的内容来简化,我只是使用了上面给出的内容
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:
Can simplify by making the toreplace something like '/\:,' etc, i just used what was given above
您将从基线开始,而不是保存替换的结果,因此您将得到相当于
尝试以下内容的结果
You are starting over with the base line instead of saving the replaced result, thus you are getting the equivalent to
Try the following
应该使用 string.replace(str, fromStr, toStr)
Should use string.replace(str, fromStr, toStr)