Reputation: 196
I wanted to extract only date from following string. Here is variable:
file = '62-201809.csv'
I used rsplit
to get rid of file csv extension like this:
splitf = file.rsplit('.', 1)[0]
I got 62-201809
so it's okey but now i need to get rid of everything to '-' and store only 201809
into variable.How to do it?
Upvotes: 1
Views: 224
Reputation: 71580
Try using:
>>> file = '62-201809.csv'
>>> file.split('-', 1)[1].split('.')[0]
'201809'
>>>
Or use regex:
>>> import re
>>> file = '62-201809.csv'
>>> re.search('-(\d+)', file).group(1)
'201809'
>>>
Upvotes: 2
Reputation: 736
If want only use split can do that:
filen = '62-201809.csv'
number = filen.split('.')[0]
number2 = number.split('-')[1]
print(number2)
first get only number, and later the number 201809 only.
Upvotes: 0