In python 3.x how do I remove a sub-string from a string in a list?

I am making a program that reads in various pieces of information about a system and sorts it into a more concise and presentable format. The necessary information gets added to a list as it gets read, but I need to remove some data from certain list elements.

For example my list will have an element that looks like this:

['Software: 12.01 (built 2016-10-28 21:51:37+01:00)']

how do I remove everything after the 12.01 (or some other arbitrary sequence)?

Edit: I figured it out

with open('filename.txt') as f: 
    software_list = [line.rstrip('\n') for line in f if 'Software:' in line]
for i in software_list:
    final_soft_list.append(i.split('(')[0])
print(final_soft_list)

the output gave me: [Software: 12.01]

1 answer

  • answered 2018-11-08 06:12 Mayank Porwal

    You can use re module.

    Assuming you want to replace substr for all the elements of a list, create a function like below:

    import re
    
    def remove_substr(input_list, substr):
        out_list = []
        for element in input_list:
            out_list.append(re.sub(substr,'',element))
        return out_list
    

    Calling the function on the sample you posted:

    In [1325]: l
    Out[1325]: ['Software: 12.01 (built 2016-10-28 21:51:37+01:00)']
    
    In [1330]: out_list = remove_substr(l, '12.01')
    
    In [1331]: out_list
    Out[1331]: ['Software:  (built 2016-10-28 21:51:37+01:00)']
    

    If you want to remove anything after the pattern, you can do this:

    In [1354]: def remove_substr(input_list, substr):
          ...:         out_list = []
          ...:         for element in input_list:
          ...:             out_list.append(element.split(substr)[0])
          ...:         return out_list
          ...:     
    
    In [1355]: 
    
    In [1355]: remove_substr(l,'12.01')
    Out[1355]: ['Software: ']
    

    Let me know if this helps.