Python删除字符串的最后3个字符

时间:2022-03-23 23:19:01

I'm trying to remove the last 3 characters from a string in python, I don't know what these characters are so I can't use rstrip, I also need to remove any white space and convert to upper-case

我正在尝试从python中的字符串中删除最后三个字符,我不知道这些字符是什么,所以我不能使用rstrip,我还需要删除任何空格并转换为大写

an example would be:

一个例子是:

foo = "Bs12 3ab"
foo.replace(" ", "").rstrip(foo[-3:]).upper()

This works and gives me BS12 which is what I want, however if the last 4th & 3rd characters are the same I loose both eg if foo = "BS11 1AA" I just get 'BS'

这就得到了BS12这就是我想要的,但是如果最后的4和3个字符都是一样的我就把它们都去掉了比如foo = "BS11 1AA"我就得到了BS

examples of foo could be:

foo的例子可以是:

BS1 1AB
bs11ab
BS111ab

The string could be 6 or 7 characters and I need to drop the last 3 (assuming no white space)

字符串可以是6或7个字符,我需要删除最后3个字符(假设没有空格)

Any tips?

任何建议吗?

9 个解决方案

#1


150  

Removing any and all whitespace:

删除任何和所有空白:

foo = ''.join(foo.split())

Removing last three characters:

删除最后三个字符:

foo = foo[:-3]

Converting to capital letters:

转换成大写字母:

foo = foo.upper()

All of that code in one line:

所有这些代码在一行中:

foo = ''.join(foo.split())[:-3].upper()

#2


86  

It doesn't work as you expect because strip is character based. You need to do this instead:

它不像你期望的那样工作,因为条带是基于字符的。你应该这样做:

foo = foo.replace(' ', '')[:-3].upper()

#3


14  

>>> foo = "Bs12 3ab"
>>> foo[:-3]
'Bs12 '
>>> foo[:-3].strip()
'Bs12'
>>> foo[:-3].strip().replace(" ","")
'Bs12'
>>> foo[:-3].strip().replace(" ","").upper()
'BS12'

#4


5  

You might have misunderstood rstrip slightly, it strips not a string but any character in the string you specify.

您可能稍微误解了rstrip,它不是剥离一个字符串,而是剥离您指定的字符串中的任何字符。

Like this:

是这样的:

>>> text = "xxxxcbaabc"
>>> text.rstrip("abc")
'xxxx'

So instead, just use

所以,只使用

text = text[:-3] 

(after replacing whitespace with nothing)

(将空白替换为空白后)

#5


2  

I try to avoid regular expressions, but this appears to work:

我尽量避免使用正则表达式,但这似乎行得通:

string = re.sub("\s","",(string.lower()))[:-3]

字符串= re.sub(“\ s”、“(string.lower()))[3]

#6


1  

What's wrong with this?

这有什么错?

foo.replace(" ", "")[:-3].upper()

#7


1  

>>> foo = 'BS1 1AB'
>>> foo.replace(" ", "").rstrip()[:-3].upper()
'BS1'

#8


0  

Aren't you performing the operations in the wrong order? You requirement seems to be foo[:-3].replace(" ", "").upper()

你不是按错误的顺序执行操作吗?你的要求似乎是foo[:-3]。替换(" "," ").upper()

#9


0  

It some what depends on your definition of whitespace. I would generally call whitespace to be spaces, tabs, line breaks and carriage returns. If this is your definition you want to use a regex with \s to replace all whitespace charactors:

它取决于你对空格的定义。我通常将空格称为空格、制表符、换行符和回车符。如果这是您的定义,您希望使用带\s的regex来替换所有空格字符:

import re

def myCleaner(foo):
    print 'dirty: ', foo
    foo = re.sub(r'\s', '', foo)
    foo = foo[:-3]
    foo = foo.upper()
    print 'clean:', foo
    print

myCleaner("BS1 1AB")
myCleaner("bs11ab")
myCleaner("BS111ab")

#1


150  

Removing any and all whitespace:

删除任何和所有空白:

foo = ''.join(foo.split())

Removing last three characters:

删除最后三个字符:

foo = foo[:-3]

Converting to capital letters:

转换成大写字母:

foo = foo.upper()

All of that code in one line:

所有这些代码在一行中:

foo = ''.join(foo.split())[:-3].upper()

#2


86  

It doesn't work as you expect because strip is character based. You need to do this instead:

它不像你期望的那样工作,因为条带是基于字符的。你应该这样做:

foo = foo.replace(' ', '')[:-3].upper()

#3


14  

>>> foo = "Bs12 3ab"
>>> foo[:-3]
'Bs12 '
>>> foo[:-3].strip()
'Bs12'
>>> foo[:-3].strip().replace(" ","")
'Bs12'
>>> foo[:-3].strip().replace(" ","").upper()
'BS12'

#4


5  

You might have misunderstood rstrip slightly, it strips not a string but any character in the string you specify.

您可能稍微误解了rstrip,它不是剥离一个字符串,而是剥离您指定的字符串中的任何字符。

Like this:

是这样的:

>>> text = "xxxxcbaabc"
>>> text.rstrip("abc")
'xxxx'

So instead, just use

所以,只使用

text = text[:-3] 

(after replacing whitespace with nothing)

(将空白替换为空白后)

#5


2  

I try to avoid regular expressions, but this appears to work:

我尽量避免使用正则表达式,但这似乎行得通:

string = re.sub("\s","",(string.lower()))[:-3]

字符串= re.sub(“\ s”、“(string.lower()))[3]

#6


1  

What's wrong with this?

这有什么错?

foo.replace(" ", "")[:-3].upper()

#7


1  

>>> foo = 'BS1 1AB'
>>> foo.replace(" ", "").rstrip()[:-3].upper()
'BS1'

#8


0  

Aren't you performing the operations in the wrong order? You requirement seems to be foo[:-3].replace(" ", "").upper()

你不是按错误的顺序执行操作吗?你的要求似乎是foo[:-3]。替换(" "," ").upper()

#9


0  

It some what depends on your definition of whitespace. I would generally call whitespace to be spaces, tabs, line breaks and carriage returns. If this is your definition you want to use a regex with \s to replace all whitespace charactors:

它取决于你对空格的定义。我通常将空格称为空格、制表符、换行符和回车符。如果这是您的定义,您希望使用带\s的regex来替换所有空格字符:

import re

def myCleaner(foo):
    print 'dirty: ', foo
    foo = re.sub(r'\s', '', foo)
    foo = foo[:-3]
    foo = foo.upper()
    print 'clean:', foo
    print

myCleaner("BS1 1AB")
myCleaner("bs11ab")
myCleaner("BS111ab")