简体   繁体   中英

Find index position of characters in string

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?

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

I'd suggest join ing 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)

Python string.index

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

    Like find() but raise ValueError when the substring is not found.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM