英文:
Removing a File Extention from a String in Python (Reverse Indexing)
问题
在最近的一个项目中,我需要从字符串中删除“.txt”扩展名。我尝试了多种方法,包括使用.find()
函数、.index()
和.search()
函数,但都没有成功。因此,我写了以下代码:
file_name = 'myFile.txt is a file'
new_name = file_name[:(len(file_name) - len(file_name.split('.')[len(file_name.split('.')) - 1]) - 1)]
print(new_name)
输出
myFile
这个方法运行良好。
不过,我在想是否有更简单和更清晰的方法可以使用逆向索引来完成,不仅适用于扩展名,还适用于其他类似的情况。不知何故,我在其他帖子中找不到有效的方法。任何帮助将不胜感激:)
英文:
In a recent project I had to remove the '.txt' extention in a string. I tried using the .find()
function as well as .index()
and .search()
in many ways but nothing came out of it. So, I wrote the following:
file_name = 'myFile.txt is a file'
new_name = file_name[:(len(file_name) - len(file_name.split('.')[len(file_name.split('.')) - 1]) - 1)]
print(new_name)
Output
myFile
It worked fine.
However I was wondering if there was something simpler and cleaner that I could do with reverse indexing... Something that could work not only with extentions but in other similar cases as well. For some reason I couldn't find something that works efficiently in other posts.
Any help would be highly appreciated : )
答案1
得分: 1
这里我看到的主要问题是,你的 file_name
对象实际上并不是文件名,它只是一个陈述文件名的语句。在这种情况下,我会采取一种不同的方法。首先,用以下代码将陈述中的单词分开:
words = file_name.split(' ')
然后你会得到一个单词列表。接着,逐个尝试找到文件名:
file = None
for word in words:
if '.' in word:
file = word.split('.')
现在你已经得到了文件名:
file_name = file[0]
file_extension = file[1]
如果你确信提供的文件名总是包含文件名和扩展名,那么你只需按照'.'进行分割,然后取索引0和1来获取名称和扩展名。
英文:
Main problem I see here is your file_name object is not really the file name, it's a statement saying the file name. In this case I would use a different approach. First, divide the words in the statement with
words = file_name.split(' ')
Then I would have a list of words. Then, one by one, I would try to find the filename
file = None
for word in words:
if '.' in word:
file = word.split('.')
Now you have the filename.
file_name = file[0]
file_extension = file[1]
If you are sure that the filename provided is always going to be filename and extension, then you only need to split it by '.' and take the index of 0 and 1 to get the name and extension.
答案2
得分: 1
我不确定你的问题具体是什么,当你说这个时:
一些不仅适用于扩展名,而且适用于其他类似情况的东西
我对你提出的实现如下。
new_name = file_name.split('.')[0]
# 或者如果可能有多个句点并且你关心最后一个
new_name = '.'.join(file_name.split('.')[:-1])
但一个更通用的解决方案将取决于那些“其他类似情况”是什么。
英文:
I'm not exactly sure what you're asking, when you say this
> Something that could work not only with extentions but in other similar cases as well
My implementation of what you presented would be the following.
new_name = file_name.split('.')[0]
# Or if multiple periods are possible and you care about the last one
new_name = '.'.join(file_name.split('.')[:-1])
But a more general solution would depend on what those "other similar cases" could be.
答案3
得分: 1
你可以简单地使用正则表达式来搜索在句点字符前面的文本:
import re
file_name = 'myFile.txt is a file'
new_name = re.findall(r'(\w+)\.', file_name)[0]
print(new_name)
英文:
You can simply use Regex to seach for the text preceeding the . character:
import re
file_name = 'myFile.txt is a file'
new_name = re.findall(r'(\w+)\.', file_name)[0]
print(new_name)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论