coderman
coderman

Reputation: 273

How can I strip unwanted characters from a string in python?

I have the following string:

text = 'adsfklaiin2007daf adf adflkajf;j 2008afadfkjkj'

I want to return:

2007 2008

Any way to do this in Python?

Upvotes: 1

Views: 1937

Answers (4)

Cédric Julien
Cédric Julien

Reputation: 80851

import re
num = re.compile('[\d]*')
numbers = [number for number in num.findall(text) if number]
['2007', '2008']

Upvotes: 5

Spencer Rathbun
Spencer Rathbun

Reputation: 14910

This is a classic case for regular expressions. Using the re python library you get:

re.findall('\d{4}', "yourStringHere")

This will return a list of all four digit items found in the string. Simply adjust your regex as needed.

Upvotes: 7

ghostdog74
ghostdog74

Reputation: 343077

>>> import re
>>> text = 'adsfklaiin2007daf adf adflkajf;j 2008afadfkjkj'
>>> re.sub("[^0-9]"," ",text)
'          2007                   2008         '

I will leave it to you to format the output.

Upvotes: 3

nmichaels
nmichaels

Reputation: 51029

str.translate

text.translate(None, ''.join(chr(n) for n in range(0xFF) if chr(n) not in ' 01234567890')

You can probably construct a better table of characters to skip and make it prettier, but that's the general idea.

Upvotes: 1

Related Questions