Remove a character and specific immediately trailing characters in python

Question:

I have a list of strings in python of the form ABC_### where ### is a number from 1-999. I want to remove the _ and any leading 0s from the number, basically any _, _0, or _00 to get the format ABC 4 or ABC 909. I can think of a couple of dumb ways to do this but no smart ways, so I’m here 🙂

Asked By: yuuuu

||

Answers:

You can use this regex:

_0*

which matches zero or more 0‘s after an _, and replace it with ' '.

import re

strs = ['ABC_2545', 'ABC_001', 'ABC_04']
for s in strs:
    print(re.sub(r'_0*', ' ', s))

Output:

ABC 2545
ABC 1
ABC 4
Answered By: Nick

As long as the format is consistent, then using string‘s split method is fine.

my_list = ['ABC_001', 'DEF_023']
new_format = (f'{letters} {int(nums)}' for letters, nums in (elem.split('_') for elem in my_list))
print(list(new_format))

Strings are returned with a generator. In this case converted to a list to be printed.

['ABC 1', 'DEF 23']

*edited to return a string formatted rather than a tuple.

Answered By: bn_ln

In case your list is not a Python list, and it’s stored in a file one line per element, try this:

import re

fi = open('myList.txt', 'r')
fo = open('myNewList.txt', 'w+')

for line in fi:
  fo.writelines(re.sub(r'_0*', ' ', line))

fi.close()
fo.close()
Answered By: Wali
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.