'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 |

my required output is

"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.

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)


Solution 1:[1]

use a regex with 1 or more spaces then a pipe then end of line.

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|'

Solution 2:[2]

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:

|ABC|DEF|GHI     |

In this case, the spaces are part of the data and the last element is not empty.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jean-François Fabre
Solution 2 Tim Biegeleisen