I have a list.
JavaScript
x
2
1
file_name = ['a.3903902.pdf','b.3432312.pdf','c.239002191.pdf','d.23423192010.pdf']
2
I want to trim the strings in the list to remove the characters including .
sign and the numbers.
Expected output:
JavaScript
1
2
1
file_name = ['a.pdf','b.pdf','c.pdf','d.pdf']
2
Advertisement
Answer
You can use regex with pattern as r'.d+'
. This will literally match .
followed by one ore more digits.
JavaScript
1
5
1
>>> import re
2
>>> file_name = ['a.3903902.pdf','b.3432312.pdf','c.239002191.pdf','d.23423192010.pdf']
3
>>> [re.sub(r'.d+', '', f) for f in file_name]
4
['a.pdf', 'b.pdf', 'c.pdf', 'd.pdf']
5