0

I would like to extrapolate the number part (379022420) from a this string:

'../input/predict-volcanic-eruptions-ingv-oe/train/379022420.csv'

I would like to do it specifiyng the initial value and the final value, i.e.

start = train/
end = .csv

this because I need to use it for several others strings and I would like to do it through a loop.

domiziano
  • 440
  • 3
  • 13
  • So is always going to be a number? Or could it be anything? – Dani Mesejo Nov 01 '20 at 16:25
  • Always numbers, all the string are similar to the one showed, for this reason I would like to specify the initial value,that is always train/ and the final value, that is always .csv – domiziano Nov 01 '20 at 16:26
  • there are better ways to extract file name without extension if that is what you want – buran Nov 01 '20 at 16:27
  • @buran Ok, can you please share them with me? – domiziano Nov 01 '20 at 16:27
  • Does this answer your question? [How to get the filename without the extension from a path in Python?](https://stackoverflow.com/questions/678236/how-to-get-the-filename-without-the-extension-from-a-path-in-python) – buran Nov 01 '20 at 16:28
  • mmm not really, it is useful to remove the extension but I need just the number part, not all the string – domiziano Nov 01 '20 at 16:41

2 Answers2

1

Maybe you can search the strings with re module:

import re

strings = [
    '../input/predict-volcanic-eruptions-ingv-oe/train/379022420.csv'
    # ... other strings
]

pattern = re.compile(r'(?<=train/)(\d+)(?=\.csv)')

for s in strings:
    m = pattern.search(s)
    if m:
        print(m.group(1))

Prints:

379022420
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
1

You should use the .split() function

textList = [...]
for i in textList:
   i.split('train/')
   desiredValue = i[1].split(".")
   desiredValue = desiredValue[0]
Oliver Hnat
  • 797
  • 4
  • 19