简体   繁体   English

如何删除字符串的最后一个字符

[英]How to remove last character of a string

I have many lines similar to我有很多类似的行

"HHH |**** XYzz| *ABC*hgg|G~GG|G|HJJ|JJJ|          |"

"HHH |**** XYzz| *ABC*hgg|G~GG|G|HJJ|HHH|" 

"HHH |**** XYzz| *ABC*hgg|G~GG|G|HJJ|III|          |"

I want to remove last pipe and spaces from such lines which contains extra |我想从这些包含额外 | 的行中删除 last pipe 和空格

my required output is我需要的 output 是

"HHH |**** XYzz| *ABC*hgg|G~GG|G|HJJ|JJJ|"

"HHH |**** XYzz| *ABC*hgg|G~GG|G|HJJ|HHH|" 

"HHH |**** XYzz| *ABC*hgg|G~GG|G|HJJ|III|"

I have tried for 1 string but the problem is this also eliminates the spaces present inside that string.我试过 1 个字符串,但问题是这也消除了该字符串中存在的空格。

A=  "HHH |**** XYzz| *ABC*hgg|G~GG|G|HJJ|JJJ|          |"
Y=A.split()
print(Y)

final=[]

if Y[-1]=='~':
    ab=Y[:-1]
    cd=''.join(ab)
    print(cd)
else:
    ef=''.join(Y)
    print(ef)

use a regex with 1 or more spaces then a pipe then end of line.使用带有 1 个或多个空格的正则表达式,然后是 pipe,然后是行尾。

your_string = re.sub("\s+\|$","",your_string)

testing:测试:

>>> your_string = "HHH |**** XYzz| *ABC*hgg|G~GG|G|HJJ|JJJ|          |"
>>> re.sub("\s+\|$","",your_string)
'HHH |**** XYzz| *ABC*hgg|G~GG|G|HJJ|JJJ|'
>>> your_string = "HHH |**** XYzz| *ABC*hgg|G~GG|G|HJJ|JJJ|"
>>> re.sub("\s+\|$","",your_string)
'HHH |**** XYzz| *ABC*hgg|G~GG|G|HJJ|JJJ|'

I would use a regex replacement here:我会在这里使用正则表达式替换:

inp = "HHH |**** XYzz| *ABC*hgg|G~GG|G|HJJ|JJJ|          |"
output = re.sub(r'(?<=\|)\s*\|$', '', inp)
print(output)  # HHH |**** XYzz| *ABC*hgg|G~GG|G|HJJ|JJJ|

The regex pattern used here says to match:此处使用的正则表达式模式表示匹配:

(?<=\|)  assert that last pipe separated column is empty
\s*      match zero or more whitespace characters
\|       match final pipe
$        end of the input

The lookbehind (?<=\|) ensures that we don't strip away the final pipe for something like this:后面的(?<=\|)确保我们不会为这样的事情去掉最后的 pipe :

|ABC|DEF|GHI     |

In this case, the spaces are part of the data and the last element is not empty.在这种情况下,空格是数据的一部分,最后一个元素不为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM