-1
list= ['/xyz/abc/def/123621912.csv',
 '/xyz/abc/def/1662828716.csv',
 '/xyz/abc/def/1111111111.csv',
 '/xyz/abc/def/7272718119.csv',
 '/xyz/abc/def/92881991911.csv',
 '/xyz/abc/def/66271819112.csv',
 '/xyz/abc/def/2717178192.csv']

This is a list . I want to extract a list having only integers

list = ['123621912,1662828716,1111111111,7272718119,92881991911,66271819112,2717178192]

I tried the following code:

files = [i.split('.csv', 1)[0] for i in files]

I got

list= ['/xyz/abc/def/123621912',
 '/xyz/abc/def/1662828716',
 '/xyz/abc/def/1111111111',
 '/xyz/abc/def/7272718119',
 '/xyz/abc/def/92881991911',
 '/xyz/abc/def/6627181911',
 '/xyz/abc/def/2717178192']

I couldn't proceed further.

  • 1
    "I couldn't proceed further" just split again by `/`? – tobias_k Jul 25 '18 at 14:28
  • Possible duplicate of [Python: removing characters except digits from string](https://stackoverflow.com/questions/1450897/python-removing-characters-except-digits-from-string) – KenHBS Jul 25 '18 at 14:30

5 Answers5

0

Your code was close, though I would probably split on the slashes instead of the .csv, select the last element of the list that split() returns, and just remove the .csv from that.

int_list = [path.split('/')[-1].replace('.csv', '') for path in list]

Sam Holloway
  • 121
  • 3
0

With regexp (note, don't use list as variable name, it shadows built-in function):

lst= ['/xyz/abc/def/123621912.csv',
 '/xyz/abc/def/1662828716.csv',
 '/xyz/abc/def/1111111111.csv',
 '/xyz/abc/def/7272718119.csv',
 '/xyz/abc/def/92881991911.csv',
 '/xyz/abc/def/66271819112.csv',
 '/xyz/abc/def/2717178192.csv']

import re
from pprint import pprint

pprint([re.findall(r'(\d+)\.', l)[0] for l in lst])

Prints:

['123621912',
 '1662828716',
 '1111111111',
 '7272718119',
 '92881991911',
 '66271819112',
 '2717178192']
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0
numList = []

for i in range(len(listA)):
    x = listA[i].split('def/', 1)
    x = x[1].split('.', 1)
    numList.append(x[0])

Output:

['123621912',
 '1662828716',
 '1111111111',
 '7272718119',
 '92881991911',
 '66271819112',
 '2717178192']
Ankur Sinha
  • 6,473
  • 7
  • 42
  • 73
0
>>> import re
>>> re.findall(r'\d+', '\n'.join(lst))
['123621912', '1662828716', '1111111111', '7272718119', '92881991911', '66271819112', '2717178192']

By the way, avoid naming your as list as it contradicts with the builtin

Sunitha
  • 11,777
  • 2
  • 20
  • 23
0

I guess that you want achieve

lst= ['123621912','1662828716','1111111111','7272718119','92881991911','66271819112','2717178192']

Try with Regular Expressions

For example:

lst2=[]
for entry in list:
  list2.append(re.search("\d+",entry).group(0)

Maybe it's not perfect but it works.

szogoon
  • 470
  • 3
  • 15