Search code examples
pythonstringliststrip

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 with 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!


Solution

  • 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.