-3

I am trawling through a storage area and the paths look a lot like this: storagearea/storage1/ABC/ABCDEF1/raw/2013/05/ABCFGM1

I wont always know what year is it. I need to find the starting index position of the year

Therefor I am looking for where I find the following in the file name (2010, 2011, 2012, 2013, 2014 etc...)

I have set up a list as follows:

list_ = ['2010', '2011','2012','2013','2014', '2015', '2016']

and I can find if it is in the file name

if any(word in file for word in list_): 
    print 'Yahooo'

But how do I find the character index of the year in the absolute path?

Kate N
  • 11
  • 2
  • you want to use `index` to find the ordinal position of an element: `list_.index('2010')` – EdChum Sep 16 '16 at 13:15
  • @EdChum I don't think this is a dupe of that question (although it's probably a dupe of some other). The main point of the question seems to be that OP does not know which of the year-strings is contained in the filename. – tobias_k Sep 16 '16 at 13:17
  • Yes exactly tobias_k – Kate N Sep 16 '16 at 13:18
  • "find the starting index position of 2010" is ambiguous. The index of `'2010'` in what, and in what way? – TigerhawkT3 Sep 16 '16 at 13:19
  • Edit the relevant information into the question; don't dump it into a comment. – TigerhawkT3 Sep 16 '16 at 13:22
  • @tobias_k yes I've reopened this – EdChum Sep 16 '16 at 13:24
  • Still ambiguous. You want to know which year was found? Or you want to know the character index of `'2010'` in that absolute path? Or you want to know how many directories deep it is? Or what? Again, add it to the question, not a comment. – TigerhawkT3 Sep 16 '16 at 13:26

3 Answers3

1

Instead of using a generator expression (which has its own scope), use a traditional loop and then print the found word's index and break when you find a match:

list_ = ['2010', '2011','2012','2013','2014', '2015', '2016']
for word in list_:
    if word in file:
        print file.index(word)
        break
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
1

I'd suggest joining those years to a regular expression using '|' as a delimiter...

>>> list_ = ['2010', '2011','2012','2013','2014', '2015', '2016']
>>> p = "|".join(list_)
>>> p
'2010|2011|2012|2013|2014|2015|2016'

... and then using re.search to find a match and span() and group() to find the position of that match and the matched year itself:

>>> filename = "storagearea/storage1/ABC/ABCDEF1/raw/2013/05/ABCFGM1"
>>> m = re.search(p, filename)
>>> m.group()
'2013'
>>> m.span()
(37, 41)
tobias_k
  • 81,265
  • 12
  • 120
  • 179
0

Python string.index

string.index(s, sub[, start[, end]])¶

    Like find() but raise ValueError when the substring is not found.
RedX
  • 14,749
  • 1
  • 53
  • 76