Home > front end >  Remove a character and specific immediately trailing characters in python
Remove a character and specific immediately trailing characters in python

Time:10-25

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 :)

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

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()
  • Related