如何删除python中字符串的最后一个字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33940917/
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
How can I remove the last character of a string in python?
提问by
I have a file path as a string and trying to remove the last '/' from the end.
我有一个作为字符串的文件路径,并试图从末尾删除最后一个 '/'。
my_file_path = '/home/ro/A_Python_Scripts/flask-auto/myDirectory/scarlett Johanson/1448543562.17.jpg/'
I've been trying it with regex but it just keeps removing all the '/'. Is there any easier way to just remove the last character without regex?
我一直在用正则表达式尝试它,但它只是不断删除所有的“/”。有没有更简单的方法来删除没有正则表达式的最后一个字符?
回答by Alasdair
As you say, you don't need to use a regex for this. You can use rstrip
.
正如您所说,您不需要为此使用正则表达式。您可以使用rstrip
.
my_file_path = my_file_path.rstrip('/')
If there is more than one /
at the end, this will remove all of them, e.g. '/file.jpg//'
-> '/file.jpg'
. From your question, I assume that would be ok.
如果最后有多个/
,这将删除所有这些,例如'/file.jpg//'
-> '/file.jpg'
。从你的问题来看,我认为这没问题。
回答by joksnet
You could use String.rstrip
.
你可以使用String.rstrip
.
result = string.rstrip('/')
回答by Yann Vernier
To remove the last character, just use a slice: my_file_path[:-1]
. If you only want to remove a specific set of characters, use my_file_path.rstrip('/')
. If you see the string as a file path, the operation is os.path.dirname. If the path is in fact a filename, I rather wonder where the extra slash came from in the first place.
要删除最后一个字符,只需使用切片:my_file_path[:-1]
。如果您只想删除一组特定的字符,请使用my_file_path.rstrip('/')
. 如果您将字符串视为文件路径,则操作为os.path.dirname。如果路径实际上是一个文件名,我就想知道额外的斜线是从哪里来的。
回答by sarath joseph
The easiest is
最简单的是
as @greggo pointed out
正如@greggo 指出的那样
string="mystring";
string[:-1]
回答by djangoliv
For a path use os.path.abspath
对于路径使用 os.path.abspath
import os
print os.path.abspath(my_file_path)
回答by SIslam
No need to use expensive regex
, if barely needed then try-
Use r'(/)(?=$)'
pattern that is capture last /
and replace with r''
i.e. blank character.
无需使用昂贵的regex
,如果几乎不需要,则尝试使用r'(/)(?=$)'
最后捕获的模式/
并替换为r''
空白字符。
>>>re.sub(r'(/)(?=$)',r'','/home/ro/A_Python_Scripts/flask-auto/myDirectory/scarlett Johanson/1448543562.17.jpg/')
>>>'/home/ro/A_Python_Scripts/flask-auto/myDirectory/scarlett Johanson/1448543562.17.jpg'
回答by tglaria
Answering the question: to remove the last character, just use:string = string[:-1]
.
回答问题:要删除最后一个字符,只需使用:string = string[:-1]
。
If you want to remove the last '\' if there is one (or if there is more than one):
如果你想删除最后一个 '\' 如果有一个(或者如果有多个):
while string[-1]=='\':
string = string[:-1]
If it's a path, then use the os.path
functions:
如果是路径,则使用以下os.path
函数:
dir = "dir1\dir2\file.jpg\" #I'm using windows by the way
os.path.dirname(dir)
although I would 'add' a slash in the end to prevent missing the filename in case there's no slash at the end of the original string:
尽管我会在末尾“添加”一个斜杠以防止丢失文件名,以防原始字符串的末尾没有斜杠:
dir = "dir1\dir2\file.jpg"
os.path.dirname(dir + "\")
When using abspath, (if the path isn't absolute I guess,) will add the current working directory to the path.
使用 abspath 时,(如果路径不是绝对路径,我猜)会将当前工作目录添加到路径中。
os.path.abspath(dir)
回答by Roshan Shibu
The simplest way is to use slice. If x is your string variable then x[:-1] will return the string variable without the last character. (BTW, x[-1] is the last character in the string variable) You are looking for
最简单的方法是使用切片。如果 x 是您的字符串变量,则 x[:-1] 将返回没有最后一个字符的字符串变量。(顺便说一句,x[-1] 是字符串变量中的最后一个字符)您正在寻找
my_file_path = '/home/ro/A_Python_Scripts/flask-auto/myDirectory/scarlett Johanson/1448543562.17.jpg/' my_file_path = my_file_path[:-1]
my_file_path = '/home/ro/A_Python_Scripts/flask-auto/myDirectory/scarlett Johanson/1448543562.17.jpg/' my_file_path = my_file_path[:-1]