Home > Software design >  How to trim the characters in the string?
How to trim the characters in the string?

Time:11-23

I have a list.

file_name = ['a.3903902.pdf','b.3432312.pdf','c.239002191.pdf','d.23423192010.pdf']

I want to trim the strings in the list to remove the characters including . sign and the numbers.

Expected output:

file_name = ['a.pdf','b.pdf','c.pdf','d.pdf']

CodePudding user response:

file_name = ['{}.{}'.format(x.split('.')[0], x.split('.')[2]) for x in file_name]

CodePudding user response:

You can use regex

>>> import re
>>> file_name = ['a.3903902.pdf','b.3432312.pdf','c.239002191.pdf','d.23423192010.pdf']
>>> [re.sub(r'\.\d ', '', f) for f in file_name]
['a.pdf', 'b.pdf', 'c.pdf', 'd.pdf']
  • Related