there is a line:

1111; CD_COUNTRY_2; DE; DE; Германия; ГЕРМАНИЯ; 523; ; ГЕРМАНИЯ ; ; ; 

How to remove a space after a character ;

Closed due to the fact that off-topic participants are rjhdby , pavel , Alexander Petrov , Kromster , cheops 20 Oct '16 at 5:21 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - rjhdby, pavel, Alexander Petrov, Kromster, cheops
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    Have you tried to do something yourself? And if so, what did not work out? - Avernial
  • I think the answer to this question will help you.stackoverflow.com/questions/502573/… - Alex.B
  • I already looked, there it is a question of removal of all characters, after a comma. - Dmitry Smirnov
  • 2
    re.sub('\;\s+',';', my_string) - remove any number of spaces after ; - MaxU
  • Exactly what is needed! Thanks - Dmitry Smirnov

1 answer 1

Try this:

 import re In [32]: s Out[32]: '1111; CD_COUNTRY_2; DE; DE; Германия; ГЕРМАНИЯ; 523; ; ГЕРМАНИЯ ; ; ; ' In [33]: re.sub('\;\s+',';', s) Out[33]: '1111;CD_COUNTRY_2;DE;DE;Германия;ГЕРМАНИЯ;523;;ГЕРМАНИЯ ;;;' 

if you also need to get rid of the spaces before ; :

 In [37]: re.sub('\s*\;\s*',';', s) Out[37]: '1111;CD_COUNTRY_2;DE;DE;Германия;ГЕРМАНИЯ;523;;ГЕРМАНИЯ;;;'