Aquarius24
Aquarius24

Reputation: 1866

How to get string before hyphen

I have below filename:

pagecounts-20150802-000000

I want to extract the date out of above 20150802 I am using the below code but its not working:

print os.path.splitext("pagecounts-20150802-000000")[0]

Upvotes: 5

Views: 7810

Answers (1)

alexgolec
alexgolec

Reputation: 28252

The methods in os.path are mainly used for path string manipulation. You want to use string splitting:

print 'pagecounts-20150802-000000'.split('-')[1]

Upvotes: 19

Related Questions