Deleting White spaces from a list in python

Viewed 43

I want to replace back slashes in a string with forward slashes , but the following points needed to be followed while doing this :

  1. If there are 2 or more consecutive back slashes , then all of it should be considered as a single back slash and needed to be converted to a forward slash.

    For example:

    Input :          r'Removing\\\white\\spaces\\from\\a\\list'
    
    Output I need : 'Removing/white/spaces/from/a/list'
    
  2. If there is one or more blank or white spaces in between 2 backslash , then the part of the string after the white spaces should be removed from the output. That is only need to replace the back slashes till the white space .

    For example:

    Input  : r'Removing\\white\\  \\spaces\\from\\a\\list'
    
    Output : Removing/white
    

I have tried the following but it don't give the correct output if there are 3 consecutive back slashes and also throws error while I try to delete the white spaces :

def function(String,SeparIn,SeparOut): 
     TAG = String.split(SeparIn)
     for i in range(len(TAG)):
          if len(TAG[i])==0:
               TAG.remove(TAG[i])
     for i in range(len(TAG)):        
          if TAG[i]=="  " :
               del TAG[i:len(TAG)]


     # initialize an empty string
     RESULT =SeparOut

     # traverse in the string  
     RESULT=RESULT.join(TAG) #joining the string elements by the new delimiter 

     # return string  
  return RESULT
  String=r'Removing\\  \\white\\spaces\\from\\a\\list'
  SeparIn=r'\\'
  SeparOut='/'
  function(String,SeparIn,SeparOut)

The above code prints the following output if there are 3 consecutive back slashes :

input:
    String=r'Removing\\\white\\spaces\\from\\a\\list'
    SeparIn=r'\\'
    SeparOut='/'
    function(String,SeparIn,SeparOut)

Output my code prints:
    'Removing/\\white/spaces/from/a/list'

The output that I want :
     'Removing/white/spaces/from/a/list'
      

The above code throws the following error if there is white spaces :

     IndexError                                Traceback (most recent call last)
    <ipython-input-88-37b8d8f131c7> in <module>()
      20 SeparIn=r'\\'
      21 SeparOut='/'
 ---> 22 function(String,SeparIn,SeparOut)

     <ipython-input-88-37b8d8f131c7> in function(String, SeparIn, SeparOut)
       5             TAG.remove(TAGELEM[i])
       6     for i in range(len(TAG)):
 ----> 7         if TAG[i]=="  " :
       8             del TAG[i:len(TAG)]
       9 

  IndexError: list index out of range

Please do help if anyone knows a solution for this .Thanks in advance !!

1 Answers

You can use a regex with re.sub, split, and strip:

def func(s):
    import re
    return re.sub('\\+', '/', s.split()[0]).strip('/')
>>> func('Removing\\\white\\spaces\\from\\a\\list')
'Removing/white/spaces/from/a/list'

>>> func(r'Removing\\white\\  \\spaces\\from\\a\\list')
'Removing/white'

as a function with parameters

def func(String, SeparIn='\\', SeparOut='/'):
    SeparIn = re.escape(SeparIn)
    return re.sub(f'{SeparIn}+', SeparOut, String.split()[0]).strip('/')

func('Removing\\\white\\ \\spaces\\from\\a\\list')
# 'Removing/white'
Related