0

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!

joaquin
  • 82,968
  • 29
  • 138
  • 152
tookanstoken
  • 283
  • 1
  • 5
  • 16

5 Answers5

3

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.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Thanks a lot! It works! I'm starting to see. So far i've performed stripping on datas from readline() so there's probably the difference. But what's the difference between string from readline() and the one from the list? – tookanstoken Sep 29 '11 at 21:44
3

Try this:

import re
flnm = re.sub('[^0-9]', '', flnm)
Jonathan Callen
  • 11,301
  • 2
  • 23
  • 44
2

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

flnm.translate(None, ' .:')
Austin Marshall
  • 2,991
  • 16
  • 14
2

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.

Community
  • 1
  • 1
Steven Rumbalski
  • 44,786
  • 9
  • 89
  • 119
0
res = reduce( lambda a, d: a.replace(d,''),[':','.',' '],flnm)
Ivan
  • 1,103
  • 2
  • 10
  • 15