如何删除python中字符串的最后一个字符?(复制)

时间:2021-12-06 00:02:07

This question already has an answer here:

这个问题已经有了答案:

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?

我一直在用regex尝试它,但是它总是删除所有的'/'。有没有更简单的方法删除没有regex的最后一个字符?

8 个解决方案

#1


10  

As you say, you don't need to use a regex for this. You can use rstrip.

正如您所说,您不需要为此使用regex。您可以使用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’。从你的问题来看,我想没问题。

#2


7  

The easiest is

最简单的是

as @greggo pointed out

正如@greggo指出

string="mystring";
string[:-1]

#3


2  

You could use String.rstrip.

您可以使用String.rstrip。

result = string.rstrip('/')

#4


2  

For a path use os.path.abspath

这条小路用的是小路。

import os    
print os.path.abspath(my_file_path)

#5


0  

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.

要删除最后一个字符,只需使用slice: my_file_path[:-1]。如果您只想删除一组特定的字符,请使用my_file_path.rstrip('/')。如果将字符串视为文件路径,操作是os.path.dirname。如果路径实际上是一个文件名,我很想知道额外的斜杠从何而来。

#6


0  

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'

#7


0  

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。路径功能:

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)

#8


0  

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

最简单的方法是使用slice。如果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]

#1


10  

As you say, you don't need to use a regex for this. You can use rstrip.

正如您所说,您不需要为此使用regex。您可以使用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’。从你的问题来看,我想没问题。

#2


7  

The easiest is

最简单的是

as @greggo pointed out

正如@greggo指出

string="mystring";
string[:-1]

#3


2  

You could use String.rstrip.

您可以使用String.rstrip。

result = string.rstrip('/')

#4


2  

For a path use os.path.abspath

这条小路用的是小路。

import os    
print os.path.abspath(my_file_path)

#5


0  

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.

要删除最后一个字符,只需使用slice: my_file_path[:-1]。如果您只想删除一组特定的字符,请使用my_file_path.rstrip('/')。如果将字符串视为文件路径,操作是os.path.dirname。如果路径实际上是一个文件名,我很想知道额外的斜杠从何而来。

#6


0  

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'

#7


0  

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。路径功能:

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)

#8


0  

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

最简单的方法是使用slice。如果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]