Ecko
Ecko

Reputation: 115

regular expressions find string letters, and string letters with combination of string numbers python

x = ['86050023328185523 THE B WITH PEANUT 140G'],['86050023328185523 ABC WITH BanaNA 40G'],['86050023328185523 B B Z WITH JElly 250G']

lets say this is our variable which contains all strings. from which the output should be everyting except string numbers in a row.

desired output:

 ['THE B WITH PEANUT 140G']
 ['ABC WITH BanaNA 40G']
 ['B B Z WITH JElly 250G']

code:


x = ['86050023328185523 A B  Z  WITH PEANUT 140GB'],['86050023328185523 A B  Z  WITH PEANUT 140GB'],['86050023328185523 A B  Z  WITH PEANUT 140GB']

for e in x:
    for i in e:
        #print(type(i)) <class 'str'>
        f = re.findall(r'\d+',i)
        print(f)

result of the code:

['86050023328185523', '140']
['86050023328185523', '40']
['86050023328185523', '250']

Upvotes: 0

Views: 53

Answers (2)

dawg
dawg

Reputation: 103884

With the list of sub lists of strings in your example, you can do:

>>> [[re.sub(r'^\d+[ \t]*','',e) for e in sl] for sl in x]
[['THE B WITH PEANUT 140G'], ['ABC WITH BanaNA 40G'], ['B B Z WITH JElly 250G']]

The [ \t] is equivalent to POSIX [:blank:] for horizontal space characters; you can use \s for all whitespace which is defined as [ \t\r\n\v\f] in ASCII.

Upvotes: 0

yatu
yatu

Reputation: 88236

I believe a list comprehension with string methods should be enough:

[[' '.join(s for s in l[0].split() if not s.isnumeric())] for l in x]

[['THE B WITH PEANUT 140G'],
 ['ABC WITH BanaNA 40G'],
 ['B B Z WITH JElly 250G']]

Upvotes: 2

Related Questions