开发者

python stripping chars from list item

I'm trying to strip characters from timestamp stored in matrix (or list of lists if you want). I want to extract the item and use it as a filename after stripping unwanted characters (it should stay unchanged in the original list). I'm quite familiar with stripping and other string operations I use it routinely, but now I stucked wit开发者_运维百科h this, I don't know what's happening, I've tried almost everything. I want to get '2011092723492' instead of original '2011.09.27 23:49 2'. In this example is just ':' to be replaced to make it easier. Am I missing something ?:

for x in FILEmatrix:
    flnm = str(x[4])               # or just 'x[4]' doesn't matter it is a string for sure
    print type(flnm)               # <type 'str'> OK, not a list or whatever
    print 'check1: ', flnm         # '2011.09.27 23:49 2'
    flnm.strip(':')                # i want to get '2011092723492', ':' for starters, but...
    print 'check2: ', flnm         # nothing... '2011.09.27 23:49 2'
    string.strip(flnm,':')
    print 'check3: ', flnm         # nothing... '2011.09.27 23:49 2'
    flnm = flnm.strip(':')
    print 'check4: ', flnm         # nothing... '2011.09.27 23:49 2'
    flnm.replace(':', '')
    print 'check5: ', flnm         # nothing... '2011.09.27 23:49 2' 

thanks a lot!


That's not what str.strip() does, and that's not how it works. Strings are immutable, so the result is returned from the method.

flnm = flnm.replace(':', '')

Repeat with the other characters you want to remove.


Try this:

import re
flnm = re.sub('[^0-9]', '', flnm)


This is pretty fast (if you want to strip those known non-alpha chars from the string):

flnm.translate(None, ' .:')


In Python there is usually more than one way to do it.

But first, strip doesn't work the way you think it does.

>>> flnm = '2011.09.27 23:49 2'
>>> flnm.strip('2') # notice that strip only affects the ends of the string
'011.09.27 23:49 '

You could join characters that aren't rejected.

rejected = ' :.'
flnm = ''.join(c for c in flnm if c not in rejected)

Or just join digit characters.

flnm = ''.join(c for c in flnm if c.isdigit())

Or chain a number of calls to string.replace.

flnm = flnm.replace(' ','').replace('.','').replace(':','')

Or use re.sub.

import re
flnm = re.sub('\D', '', flnm)

Since strings are immutable, make sure you assign the result back to flnm.

Edit:

Even more ways to do it!

Using reduce (from Ivan's answer):

rejected = ' :.'
flnm = reduce(lambda a, d: a.replace(d,''), rejected, flnm)

Using translate (from oxtopus's answer):

rejected = ' :.'
flnm = flnm.translate(None, rejected)

I prefer oxtopus's use of translate as it's the most straight forward.


res = reduce( lambda a, d: a.replace(d,''),[':','.',' '],flnm)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜