Home > front end >  How to strip certain characters from a string in python
How to strip certain characters from a string in python

Time:02-10

I am trying to strip certain characters from a string. The string I have is -

test_list = ['INFO:      196ns ### Starting ABC Test ###\n', 'INFO:     310ns ### Starting write_def_test ###\n', 'INFO:     752ns ### Starting write_55s_test ###\n', 'INFO:    152ns ### Starting rands_test ###\n', 'INFO:    237ns ### Starting dog_wffs_test ###\n']

The code I have is -

test_list_1 = []
for i in test_list:
    m = i.strip("INFO:      ")
    m = m.strip("###")
    test_list_1.append(m)
print(test_list_1)

The output I am looking for is -

['ABC Test', 'write_def_test', 'write_55s_test', 'rands_test', 'dog_wffs_test']

How can I get it?

CodePudding user response:

You're better off using regex here, i.e.:

import re

out = re.findall('Starting ([^#] )', '|'.join(test_list))

CodePudding user response:

Here's one using regex:

import re

test_list = ['INFO:      196ns ### Starting ABC Test ###\n', 'INFO:     310ns ### Starting write_def_test ###\n', 'INFO:     752ns ### Starting write_55s_test ###\n', 'INFO:    152ns ### Starting rands_test ###\n', 'INFO:    237ns ### Starting dog_wffs_test ###\n']

p = re.compile(r"### Starting (.*?) ###")
output = [next(p.finditer(x)).group(1) for x in test_list]
# or, output = [p.findall(x)[0] for x in test_list]

print(output)
# ['ABC Test', 'write_def_test', 'write_55s_test', 'rands_test', 'dog_wffs_test

This approach is to get the "core" part, rather than stripping out peripheral parts.

CodePudding user response:

I agree regex is probably the way to go for complex pattern matching, if you cant use that or don't want to learn it for whatever reason, the string.find() method is very useful for simple cases like this/one offs where cooking up regex may take a little too much time:

test_list_1 = []
for i in test_list:
    test_list_1.append(i[i.find('### ')   13 : i.find('###\n') - 1])
print(test_list_1)
  • Related