2

I have a list of strings and i would like to extract : "000000_5.612230" of :

A = '/calibration/test_min000000_5.612230.jpeg'

As the size of the strings could evolve, I try with monitoring the position of "n" of "min". I try to get the good index with :

print sorted(A, key=len).index('n')

But i got "11" which corresponds to the "n" of "calibration". I would like to know how to get the maximum index value of the string?

user3601754
  • 3,792
  • 11
  • 43
  • 77

5 Answers5

4

it is difficult to answer since you don't specify what part of the filename remains constant and what is subject to change. is it always a jpeg? is the number always the last part? is it always preceded with '_min' ?

in any case, i would suggest using a regex instead:

import re

A = '/calibration/test_min000000_5.612230.jpeg'
p = re.compile('.*min([_\d\.]*)\.jpeg')
value = p.search(A).group(1)
print value

output :

000000_5.612230

note that this snippet assumes that a match is always found, if the filename doesn't contain the pattern then p.search(...) will return None and an exception will be raised, you'll check for that case.

yurib
  • 8,043
  • 3
  • 30
  • 55
2

You can use re module and the regex to do that, for example:

import re

A = '/calibration/test_min000000_5.612230.jpeg'
text = re.findall('\d.*\d', A)

At now, text is a list. If you print it the output will be like this: ['000000_5.612230']

So if you want to extract it, just do this or use for:

import re

A = '/calibration/test_min000000_5.612230.jpeg'
text = re.findall('\d.*\d', A)

print text[0]
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
1

String slicing seems like a good solution for this

>>> A = '/calibration/test_min000000_5.612230.jpeg'
>>> start = A.index('min') + len('min')
>>> end = A.index('.jpeg')
>>> A[start:end]
'000000_5.612230'

Avoids having to import re

Tim
  • 41,901
  • 18
  • 127
  • 145
  • You probably want the reverse index, [**`rindex`**](https://docs.python.org/2/library/stdtypes.html#str.rindex) to find the last `min` and the last `.jpeg`, although the last `.jpeg` will always be at `-5`. – Peter Wood Sep 16 '15 at 08:41
1

Try this (if extension is always '.jpeg'):

A.split('test_min')[1][:-5]
Julien
  • 13,986
  • 5
  • 29
  • 53
0

If your string is regular at the end, you can use negative indices to slice the string:

>>> a = '/calibration/test_min000000_5.612230.jpeg'
>>> a[-20:-5]
'000000_5.612230'
Community
  • 1
  • 1
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
  • It seems from your question that the start is not regular but the end is. So you always have the 15 characters you want followed by `.jpeg`. – Peter Wood Sep 16 '15 at 08:33